2012-09-03 69 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"); 
} 
相關問題