jQuery Ajax 调用了两次

问题描述 投票:0回答:1

我有一个像这样的

<input>
标签:

<input type="button" id="btnSave2" value="Save" onclick="Event(1)" class="btn btn-primary col-lg-12" />

这是我的脚本:

function event(loop){
  debugger
  $.ajax({
    method: "POST",
    url: "UpEven.aspx",
    data: { slno: EventNo[0]}
  }).done(function(msg){
    debugger
  })
}

单击 Save 按钮后,Ajax 被调用两次。初始调试器仅被命中一次,但 Ajax 渲染了两次。

我希望 Ajax 在每次单击按钮时只调用一次。

javascript jquery ajax events frontend
1个回答
0
投票

$(document).ready(function () {
   $('#btnSave2').click(function () {
      Event(1);
   });
});

function Event(loop) {
   debugger
   $.ajax({
      method: "POST",
      url: "UpEven.aspx",
      data: { slno: EventNo[0] }
   }).done(function (msg) {
      debugger
   });
}
/* 

Remove Inline Onclick Attribute:

If you're using jQuery, you can remove the inline onclick attribute from the HTML and bind the event using jQuery. This can help in avoiding any conflicts 

please remove Event(1) function if you write outside a function called.

*/

<input type="button" id="btnSave2" value="Save" class="btn btn-primary col-lg-12" />

© www.soinside.com 2019 - 2024. All rights reserved.