2011-12-12 109 views
0

我有一个灯箱形式在框中工作的新条目添加到我datatable形式与此代码:防止多个Ajax查询

$("#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个或更多的查询,我怎样才能防止这个?通常它应该只发送一个查询。

+0

是该事件'$( “#附加报​​价”)。单击(''中创建的document.ready()'?有的时候'click'事件创建的多个时间代码和你确实有这种行为 –

+0

它创建在页面末尾 – Awea

回答

2

当您单击#add-quotation时,您将另一个单击事件绑定到#quotation_submit。由于您使用的是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'); 
     } 
    }); 
}); 
+0

它很好地工作,但我不明白你为什么能解释?^^ – Awea

0

使用one()vs live()会阻止多次提交。与使用on()相同,除了处理程序在第一次事件发生后被删除。

http://api.jquery.com/one/