如何防止jquery ui确认对话框在双击时添加两条记录?
我正在尝试使用jQueryUI对话框和确认框,使用以下代码在表中添加新记录。如果我双击确认按钮,记录将在数据库中添加两次。我怎样才能防止这种情况如何防止jquery ui确认对话框在双击时添加两条记录?,jquery,jquery-ui,Jquery,Jquery Ui,我正在尝试使用jQueryUI对话框和确认框,使用以下代码在表中添加新记录。如果我双击确认按钮,记录将在数据库中添加两次。我怎样才能防止这种情况 function AddNewClient(){ jQuery("#confirmdialog").html("are you sure"); jQuery("#confirmdialog").dialog({ modal: true,
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");
}
一种客户端解决方案是添加布尔值:
var sent = false;
另一个更健壮的解决方案是在服务器端检查您是否尚未插入这些数据。我通常更愿意这样做,因为任何类型的问题或攻击都可能发生在服务器之外(在浏览器或网络上)。这就是为我所做的:
$(":button:contains('OK')").attr("disabled", true);
您可以使用jquery ui函数isOpen()
$(":button:contains('OK')").attr("disabled", true);
if( $(this).dialog("isOpen") ){
YOUR CODE
} else {
return;
}
$(this).dialog("close");
}