2012-09-03 18 views
1

我想在一个表中添加新记录用下面的代码,使用jQuery UI的对话框并确认框。如果我确认按钮双击该记录将在数据库中添加两次。我怎样才能防止这一点?如何防止添加twise记录时双击jQuery UI的确认对话框?

function AddNewClient(){  
      jQuery("#confirmdialog").html("are you sure"); 
      jQuery("#confirmdialog").dialog({ 
        modal: true, 
        buttons : { 
        "Confirm" : function() {                        
         jQuery.ajax({ 
           type: "POST",       
           url: "index.php?option=com_travelagencycrm&view=clients&task=AddNewClient&format=raw", 
           cache: false,  
           data : {id:jQuery("#client_id").val(), 
             fullname:jQuery("#fullname").val(), 
             vat_id:jQuery("#vat_id").val(),          
             address:jQuery("#address").val(), 
             state_id:jQuery("#state_name").val(), 
             country_id:jQuery("#country_name").val(), 
             email:jQuery("#email").val(), 
             phone_1:jQuery("#phone_1").val(), 
             phone_2:jQuery("#phone_2").val(), 
             postalcode:jQuery("#postalcode").val()          
           } 
          }).done(function(msg) {   

           jQuery("#tablepanelclients").flexReload(); 
           //alert(msg); 
           jQuery("#confirmdialog").dialog("close"); 
           jQuery("#editclient").dialog("close");        

          }).error(function(msg){ 
           alert(msg); 
           jQuery("#confirmdialog").dialog("close"); 
           jQuery("#editclient").dialog("close"); 
          }); 


        }, 
        "Cancel" : function() { 
         jQuery(this).dialog("close"); 
        } 
        } 
       }); 

       jQuery("#confirmdialog").dialog("open"); 

     } 

回答

1

一个客户端解决方案是增加一个布尔值:

var sent = false; 

...

buttons : { 
       "Confirm" : function() { 
        if (sent) return; 
        sent = true;                       
        jQuery.ajax({ 

另一个更强大的解决办法是做服务器端,你没检查还没有插入这些数据。我一般宁愿这是什么样的问题或攻击可以在服务器(在浏览器或网络)之外发生。

+0

是的,它工作得很好。谢谢 – themis

1

这是为我做:

$(":button:contains('OK')").attr("disabled", true); 
0

U可以使用jQuery UI的功能ISOPEN()。

if($(this).dialog("isOpen")){ 
     YOUR CODE   
    } else { 
     return; 
    } 
    $(this).dialog("close"); 
} 
相关问题