1

我正在尝试使用 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");

        }
4

3 回答 3

1

这就是为我做的:

$(":button:contains('OK')").attr("disabled", true);
于 2012-12-17T17:30:35.437 回答
1

一种客户端解决方案是添加一个 boolean :

var sent = false;

...

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

另一个更强大的解决方案是在服务器端检查您尚未插入这些数据。我通常更喜欢这个,因为任何类型的问题或攻击都可能发生在服务器之外(在浏览器或网络上)。

于 2012-09-03T16:23:00.893 回答
0

你可以使用jquery ui函数isOpen()。

 if( $(this).dialog("isOpen") ){
        YOUR CODE           
    } else {
        return;
    }
    $(this).dialog("close");
}
于 2014-07-23T09:24:05.663 回答