$.ajax 发送两次,event.preventDefault 不可用

$.ajax send twice, event.preventDefault not usable?

本文关键字:event preventDefault 两次 ajax      更新时间:2023-09-26

我刚刚学习javascript和jquery,现在我在使用jquery数据表时遇到了问题。

我的数据被发布到php两次。我不知道如何在我的脚本上实现 event.preventDefault 并返回 false 不起作用。

olistorderproduktdatatable = $("#listorderprodukttable").dataTable({
    "bJQueryUI": true, "bPaginate": false, "bFilter": true, "bInfo": false, "bAutoWidth": true, "sDom": 'Rlfrtip', 
    "bServerSide": true,
    "sSearch" :"Filter",
    "bDestroy": true,
    "sAjaxSource": "share/content/helper/datatablelistorderprodukt.php" 
});
$('#listorderprodukttable tbody').delegate("tr", "click", rowClickAddPosition);
function rowClickAddPosition(){
    //Welche Zeile wurde selektiert und ID herausfiltern 
   if (hlr)
      $("td:first", hlr).parent().children().each(function(){$(this).removeClass('markrow');});
   hlr = this;
   $("td:first", this).parent().children().each(function(){$(this).addClass('markrow');});
   // You can pull the values out of the row here if required
   var a = $("td:first", this).text();
   var b = $("td:eq(1)", this).text();
   selectedRow      = a;  //ID der Zeile WICHTIGE VARIABLE!!!
    //Abfrage ob ID leer ist, also ob es überhaupt Einträge gibt.
   if(selectedRow != "No matching records found"){
        $.ajax({
            type : 'POST',
            url : 'share/content/helper/orderaddposition.php',
            cache: false,
            data: {
                SelectedProdukt : selectedRow,
                BestellID       : BestellID
            },
            success: function(data) {
                callbackaddposition();
            }
        });
    }
}

我不知道如何赶上活动?

你试过吗:

function rowClickAddPosition(e) {
   e.preventDefault();
   // ...
}

使用 event.stopPropagation() 并在函数中使用 return false,例如,

$('#listorderprodukttable tbody').delegate("tr", "click", function(e){
    rowClickAddPosition(e);
    return false;// to stop bubbling up
});
function rowClickAddPosition(e){
   e.stopPropagation();
   // remaining code