Javascript函数重复旧调用,第二次调用时

时间:2019-04-11 04:42:26

标签: javascript ajax function

我想多次将值传递给js函数,但是在调用之后,js函数将使用旧参数执行,然后再次使用新值执行 我有html:

<a onclick="deleteConfirm(1,'abc')" href="#" class="badge badge-danger">Delete</a>
<a onclick="deleteConfirm(2,'def')" href="#" class="badge badge-danger">Delete</a>

这是JavaScript:

<script>
function deleteConfirm(id, title) {
        var delmodal = $('#deleteModal');
        delmodal.find('.modal-body').text(title);
        delmodal.modal('show');
        $('#deleteBtn').click(function () {
            delmodal.modal('hide');
            $.ajax({
            type: "POST",
            url: '@Url.Action("DeleteConfirmed", "Admin")',
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify({ id: id }),
            dataType: "json",
                success: function (recData) {
                    var notify = $.notify('<strong>Thành công</strong><br/>' + recData.Message + '<br />', {
                        type: 'pastel-info',
                        allow_dismiss: false,
                    });
                },
                error: function () {
                    var notify = $.notify('<strong>Lỗi</strong><br/>Không xóa được<br />', {
                        type: 'pastel-warning',
                        allow_dismiss: false,
                    });
                }
        });
        });
    }
</script>

第一次单击徽章,值为1,“ abc”,但是第二次单击第二个链接,该值仍为1,“ abc”,此后,它将再次调用新的值2,“ def'。

3 个答案:

答案 0 :(得分:0)

您可能已经用相同的文件名在浏览器中缓存了JS文件。您可以尝试ctrl + f5重新加载,忽略缓存的内容。

答案 1 :(得分:0)

您可以像这样向脚本源链接添加版本

$(".checkbox-menu").on("change", "input[type='checkbox']", function() { // this one to select multiple options as check box
  $(this).closest("li").toggleClass("active", this.checked);
  var sList = "";
  $('input[type=checkbox]').each(function() {
    if (this.checked) {
      sList += $(this).val() + ","
    }
  });

  $("#To").val(sList.slice(0, -1));
});

$(document).on('click', '.allow-focus', function(e) {
  e.stopPropagation();
});

var data = [{
    "billdate": "2018-08-04",
    "outlet": "JAYANAGAR",
    "gross": 490465,
    "discount": 839,
    "GST": 28465,
    "amount": 518212
  },
  {
    "billdate": "2018-08-04",
    "outlet": "MALLESHWARAM",
    "gross": 99212,
    "discount": 0,
    "GST": 5567,
    "amount": 104801
  },
  {
    "billdate": "2018-08-04",
    "outlet": "KOLAR",
    "gross": 131349,
    "discount": 0,
    "GST": 6676,
    "amount": 138151
  },
  {
    "billdate": "2018-08-05",
    "outlet": "JAYANAGAR",
    "gross": 594466,
    "discount": 591,
    "GST": 34374,
    "amount": 628358
  },
  {
    "billdate": "2018-08-05",
    "outlet": "MALLESHWARAM",
    "gross": 109029,
    "discount": 0,
    "GST": 6062,
    "amount": 115113
  },
  {
    "billdate": "2018-08-05",
    "outlet": "KOLAR",
    "gross": 127449,
    "discount": 0,
    "GST": 6511,
    "amount": 134107
  },
  {
    "billdate": "2018-08-06",
    "outlet": "JAYANAGAR",
    "gross": 167811,
    "discount": 0,
    "GST": 9968,
    "amount": 177866
  },
  {
    "billdate": "2018-08-06",
    "outlet": "KOLAR",
    "gross": 62796,
    "discount": 0,
    "GST": 3257,
    "amount": 66095
  },
  {
    "billdate": "2018-08-07",
    "outlet": "JAYANAGAR",
    "gross": 267398,
    "discount": 268,
    "GST": 15898,
    "amount": 283124
  },
  {
    "billdate": "2018-08-07",
    "outlet": "MALLESHWARAM",
    "gross": 55381,
    "discount": 0,
    "GST": 3383,
    "amount": 58789
  },
  {
    "billdate": "2018-08-07",
    "outlet": "KOLAR",
    "gross": 64586,
    "discount": 6,
    "GST": 3285,
    "amount": 67886
  }
]

let formatData = function(data) {
  let billdates = [];
  let outlets = [];
  data.forEach(element => {
    if (billdates.indexOf(element.billdate) == -1) {
      billdates.push(element.billdate);
    }
    if (outlets.indexOf(element.outlet) == -1) {
      outlets.push(element.outlet);
    }
  });
  return {
    data: data,
    billdates: billdates,
    outlets: outlets,

  };
};

let renderTable = function(data) {
  billdates = data.billdates;
  outlets = data.outlets;
  data = data.data;
  let tbl = document.getElementById("dailySales");
  let table = document.createElement("table");
  let thead = document.createElement("thead");
  let headerRow = document.createElement("tr");
  let th = document.createElement("th");
  th.innerHTML = "BillDate";
  th.classList.add("text-center");
  headerRow.appendChild(th);
  let grandTotal = 0;
  let grandGross = 0;
  let grandDiscount = 0;
  let grandGst = 0;
  let outletWiseTotal = {};
  let outletWiseGross = {};
  let outletWiseDiscount = {};
  let outletWiseGst = {};
  th = document.createElement("th");
  th.colSpan = 4;
  th.innerHTML = "Total";
  th.classList.add("text-center");
  headerRow.appendChild(th);
  outlets.forEach(element => {

    th = document.createElement("th");
    th.colSpan = 4;
    th.innerHTML = element;
    th.classList.add("text-center");
    headerRow.appendChild(th);

    outletWiseTotal[element] = 0;
    outletWiseGross[element] = 0;
    outletWiseDiscount[element] = 0;
    outletWiseGst[element] = 0;
    data.forEach(el => {
      if (el.outlet == element) {
        outletWiseTotal[element] += parseInt(el.amount);
        outletWiseGross[element] += parseInt(el.gross);
        outletWiseDiscount[element] += parseInt(el.discount);
        outletWiseGst[element] += parseInt(el.GST);
      }
    });
    grandTotal += outletWiseTotal[element]; //calculating totals for Total column
    grandGross += outletWiseGross[element];
    grandDiscount += outletWiseDiscount[element];
    grandGst += outletWiseGst[element];
  });

  thead.appendChild(headerRow);
  headerRow = document.createElement("tr");
  th = document.createElement("th");
  th.innerHTML = "";
  headerRow.appendChild(th);

  for (i = 0; i < outlets.length + 1; i++) { //here i am making the header as col-span
    th = document.createElement("th");
    th.innerHTML = "Discount";
    th.classList.add("text-center");
    th.classList.add("discount"); //adding class to column discount
    headerRow.appendChild(th);

    th = document.createElement("th");
    th.innerHTML = "GST";
    th.classList.add("text-center");
    th.classList.add("gst"); //adding class to column gst
    headerRow.appendChild(th);

    th = document.createElement("th");
    th.innerHTML = "Net Amount";
    th.classList.add("text-center");
    th.classList.add("netAmount"); //adding class to column Net Amount
    headerRow.appendChild(th);

    th = document.createElement("th");
    th.innerHTML = "Gross Amount";
    th.classList.add("text-center");
    th.classList.add("grossAmount"); //adding class to column Gross Amount
    headerRow.appendChild(th);
  }

  headerRow.insertBefore(th, headerRow.children[1]);
  thead.appendChild(headerRow);
  table.appendChild(thead);

  headerRow = document.createElement("tr");
  td = document.createElement("th");
  td.innerHTML = "Total";
  td.classList.add("text-center");
  headerRow.appendChild(td);

  outlets.forEach(element => { // these are the table rows for each column
    td = document.createElement("th");
    td.innerHTML = outletWiseGross[element].toLocaleString('en-IN');
    td.classList.add("text-right");
    headerRow.appendChild(td);

    td = document.createElement("th");
    td.innerHTML = outletWiseDiscount[element].toLocaleString('en-IN');
    td.classList.add("text-right");
    headerRow.appendChild(td);

    td = document.createElement("th");
    td.innerHTML = outletWiseGst[element].toLocaleString('en-IN');
    td.classList.add("text-right");
    headerRow.appendChild(td);

    td = document.createElement("th");
    td.innerHTML = outletWiseTotal[element].toLocaleString('en-IN');
    td.classList.add("text-right");
    headerRow.appendChild(td);


  });
  td = document.createElement("th");
  td.innerHTML = grandTotal.toLocaleString('en-IN');
  td.classList.add("text-right");
  headerRow.insertBefore(td, headerRow.children[1]);

  td = document.createElement("th");
  td.innerHTML = grandGst.toLocaleString('en-IN');
  td.classList.add("text-right");
  headerRow.insertBefore(td, headerRow.children[1]);

  td = document.createElement("th");
  td.innerHTML = grandDiscount.toLocaleString('en-IN');
  td.classList.add("text-right");
  headerRow.insertBefore(td, headerRow.children[1]);

  td = document.createElement("th");
  td.innerHTML = grandGross.toLocaleString('en-IN');
  td.classList.add("text-right");
  headerRow.insertBefore(td, headerRow.children[1]);


  thead.appendChild(headerRow);
  table.appendChild(thead);

  let tbody = document.createElement("tbody");
  billdates.forEach(element => {
    let row = document.createElement("tr");
    td = document.createElement("td");
    td.innerHTML = element;
    row.appendChild(td);

    let total = 0;
    let totalGross = 0;
    let totalDiscount = 0;
    let totalGST = 0;
    outlets.forEach(outlet => {
      let ta = 0;
      let tg = 0;
      let tdi = 0;
      let tgst = 0;
      data.forEach(d => {
        if (d.billdate == element && d.outlet == outlet) {
          total += parseInt(d.amount);
          totalGross += parseInt(d.gross);
          totalDiscount += parseInt(d.discount);
          totalGST += parseInt(d.GST);
          ta = d.amount;
          tg = d.gross;
          tdi = d.discount;
          tgst = d.GST;
        }
      });

      td = document.createElement("td");
      td.innerHTML = tg.toLocaleString('en-IN');
      td.classList.add("text-right");
      row.appendChild(td);

      td = document.createElement("td");
      td.innerHTML = tdi.toLocaleString('en-IN');
      td.classList.add("text-right");
      row.appendChild(td);

      td = document.createElement("td");
      td.innerHTML = tgst.toLocaleString('en-IN');
      td.classList.add("text-right");
      row.appendChild(td);

      td = document.createElement("td");
      td.innerHTML = ta.toLocaleString('en-IN');
      td.classList.add("text-right");
      row.appendChild(td);




    });


    td = document.createElement("td");
    td.innerHTML = total.toLocaleString('en-IN');
    td.classList.add("text-right");
    row.insertBefore(td, row.children[1]);

    td = document.createElement("td");
    td.innerHTML = totalGST.toLocaleString('en-IN');
    td.classList.add("text-right");
    row.insertBefore(td, row.children[1]);

    td = document.createElement("td");
    td.innerHTML = totalDiscount.toLocaleString('en-IN');
    td.classList.add("text-right");
    row.insertBefore(td, row.children[1]);



    td = document.createElement("td");
    td.innerHTML = totalGross.toLocaleString('en-IN');
    td.classList.add("text-right");
    row.insertBefore(td, row.children[1]);


    tbody.appendChild(row);
  });

  table.appendChild(tbody);
  tbl.innerHTML = "";
  tbl.appendChild(table);
  table.classList.add("table");
  table.classList.add("table-striped");
  table.classList.add("table-bordered");
  table.classList.add("table-hover");
}
let formatedData = formatData(data);
renderTable(formatedData);

将1500更改为您喜欢的版本号

答案 2 :(得分:0)

最后,我意识到$('#deleteBtn')。onclick将EventListener添加到元素中。

每次单击#deleteBtn时都会调用它,因此将多个事件侦听器附加到deleteBtn。这就是为什么我的ajax被多次调用的原因。

解决方案(只需将事件移出函数):

<script>
function deleteConfirm(id, title) {
        var delmodal = $('#deleteModal');
        delmodal.find('.modal-body').text(title);
        delmodal.modal('show');
    }
$('#deleteBtn').click(function () {
        delmodal.modal('hide');
        $.ajax({
        type: "POST",
        url: '@Url.Action("DeleteConfirmed", "Admin")',
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify({ id: id }),
        dataType: "json",
            success: function (recData) {
                var notify = $.notify('<strong>Thành công</strong><br/>' + recData.Message + '<br />', {
                    type: 'pastel-info',
                    allow_dismiss: false,
                });
            },
            error: function () {
                var notify = $.notify('<strong>Lỗi</strong><br/>Không xóa được<br />', {
                    type: 'pastel-warning',
                    allow_dismiss: false,
                });
            }
    });
    });
</script>