我正在尝试使用以下代码在表中添加新记录,使用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");
}
答案 0 :(得分:1)
一个客户端解决方案是添加一个布尔值:
var sent = false;
...
buttons : {
"Confirm" : function() {
if (sent) return;
sent = true;
jQuery.ajax({
另一个更强大的解决方案是在服务器端执行检查,即您尚未插入这些数据。我通常更喜欢这种情况,因为任何类型的问题或攻击都可能发生在服务器外部(在浏览器或网络上)。
答案 1 :(得分:1)
这就是为我做的事情:
$(":button:contains('OK')").attr("disabled", true);
答案 2 :(得分:0)
你可以使用jquery ui函数isOpen()。
if( $(this).dialog("isOpen") ){
YOUR CODE
} else {
return;
}
$(this).dialog("close");
}