Jquery 防止多个ajax查询
我在灯箱中有一个表单,可在灯箱中的表单中添加新条目。请使用以下代码:Jquery 防止多个ajax查询,jquery,Jquery,我在灯箱中有一个表单,可在灯箱中的表单中添加新条目。请使用以下代码: $("#add-quotation").click(function(event) { event.preventDefault(); var customer_id = $(this).attr('data-customer'); $.nmManual( '#quotation_manage',{ sizes: { // Size information w: 500,
$("#add-quotation").click(function(event) {
event.preventDefault();
var customer_id = $(this).attr('data-customer');
$.nmManual(
'#quotation_manage',{
sizes: { // Size information
w: 500, // Initial width
h: 500 // Initial height
}
});
$('#quotation_submit').live('click', function(event){
event.preventDefault();
if(typeof($.nmTop()) != "undefined"){
$.nmTop().close();
}
var loading = $('.loading-notification');
loading.removeClass('hidden');
var date = $('#date_activate').val();
var budget = $('#budget').val();
var hospital = [$('#hospital').val(), $('#hospital option:selected').text()];
var dental = [$('#hospital').val(), $('#hospital option:selected').text()];
var optical = [$('#optical').val(), $('#optical option:selected').text()];
var doctor = [$('#doctor').val(), $('#doctor option:selected').text()];
$.ajax({
type: 'POST',
data: 'create=true&customer_id=' + customer_id + '&date=' + date + '&budget=' + budget + '&hospital=' + hospital[0] + '&dental=' + dental[0] + '&optical=' + optical[0] + '&doctor=' + doctor[0],
url: 'quotation.php',
dataType: 'json',
async: false,
success: function(result){
if (result){
oTable.fnAddData([
result.id,
result.date,
budget,
hospital[1],
dental[1],
optical[1],
doctor[1],
'',
'',
'',
'test'
]);
}
loading.addClass('hidden');
}
});
});
});
它工作得很好,但有时它会发送3个或更多查询,我如何防止这种情况?通常,它应该只发送一个查询。当您单击“添加报价单”时,您将另一个单击事件绑定到“提交报价单”。因为您使用的是live
,所以您可以安全地将其移出。试试这个:
$("#add-quotation").click(function(event) {
event.preventDefault();
var customer_id = $(this).attr('data-customer');
$.nmManual('#quotation_manage', {
sizes: { // Size information
w: 500,
// Initial width
h: 500 // Initial height
}
});
});
$('#quotation_submit').live('click', function(event) {
event.preventDefault();
if (typeof($.nmTop()) != "undefined") {
$.nmTop().close();
}
var loading = $('.loading-notification');
loading.removeClass('hidden');
var date = $('#date_activate').val();
var budget = $('#budget').val();
var hospital = [$('#hospital').val(), $('#hospital option:selected').text()];
var dental = [$('#hospital').val(), $('#hospital option:selected').text()];
var optical = [$('#optical').val(), $('#optical option:selected').text()];
var doctor = [$('#doctor').val(), $('#doctor option:selected').text()];
$.ajax({
type: 'POST',
data: 'create=true&customer_id=' + customer_id + '&date=' + date + '&budget=' + budget + '&hospital=' + hospital[0] + '&dental=' + dental[0] + '&optical=' + optical[0] + '&doctor=' + doctor[0],
url: 'quotation.php',
dataType: 'json',
async: false,
success: function(result) {
if (result) {
oTable.fnAddData([
result.id, result.date, budget, hospital[1], dental[1], optical[1], doctor[1], '', '', '', 'test']);
}
loading.addClass('hidden');
}
});
});
使用one()vs live()可以防止多次提交。与使用on()相同,只是处理程序在第一次事件发生后被删除
是事件
$(“#添加引号”)。单击(
在文档中创建。就绪()
?有一段时间,单击事件在代码中多次创建,您确实有这种行为。它是在页面末尾创建的。它工作得很好,谢谢,但我不明白您为什么要解释^^