2013-07-25 24 views
0

我有一个jQuery UI对话框,第一次发送数据的自定义按钮很好,但是当我再次点击一个新打开的对话框时,它会发送两次,然后三次,等等(没有页面重新加载)。如果我重新加载页面,它工作正常,但为什么?这是我的功能如何:uidialog多次发送表格

function openPopup() { 
    $('#box').dialog({ 
     autoOpen: true, 
     modal : true, 
     title : 'my title', 
     width : 500, 
     open : function (event) { 
      var dialog = $(this); 
      $(".buttonclass").live('click', function(event) { 
       dialog.dialog('destroy'); 
       alert('hello'); //This alerts hello once first time, twice second time, etc. 
      }); 
     }, 
     buttons : [ 
      { 
       text: 'Cancel', 
       click: function() { 
        $(this).dialog("destroy"); 
       } 
      } 
     ] 
    }); 
    } 

我错过了什么?我一直在环顾四周,发现像点击解除绑定,对话框销毁,但没有任何工作(如你可以看到我破坏我的对话)。

回答

0

解决方案是在创建单击事件之前使用解除绑定。这是怎样的开放以上部分应该是这样的:

open : function (event) { 
     var dialog = $(this); 
     $(".buttonclass").unbind('click').click(function(event) { 
      dialog.dialog('destroy'); 
      alert('hello'); //This alerts hello once always now. 
     }); 
}, 

这样一来,没有多余的点击都绑定到该按钮,形式只发送一次。