HTML 按钮点击函数无法使用 jQuery 变量作为参数

HTML button onclick function not working with jQuery variable as argument

本文关键字:jQuery 变量 参数 按钮 函数 HTML      更新时间:2023-09-26
success: function(jsonresponse) {
    data = JSON.stringify(jsonresponse);
  $.each(JSON.parse(data), function (index, item) {
    var Id=item.id;
    var JobId=item.JobId;
 var eachrow = "<tr>"
             + "<td>" + item.id + "</td>"
             + "<td>" + item.JobId + "</td>"
             + "<td>" + item.UserId + "</td>"
             + "<td>" + item.updatedAt + "</td>"
             + "<td>" + "<button onclick='myFunction(JobId,Id, '"accepted'")' class='btn btn-success btn-block'>Reject</button>" + "</td>"
             + "<td>" + "<button onclick='myFunction(JobId,Id, '"rejected'")' class='btn btn-success btn-block'>Reject</button>" + "</td>"
             + "</tr>";
 $('#tbody2').append(eachrow);
 });
},

myFuntion(JobId,Id,''"accept''"(不能将变量作为参数,但是当手动添加时,该函数可以工作,就像myFuntion(1,3,''"accepted''">

(。

问题:您没有在HTML代码中连接JobIdId

溶液:在HTML代码中正确连接JobIdId变量,您将很高兴:

success: function(jsonresponse) {
    data = JSON.stringify(jsonresponse);
  $.each(JSON.parse(data), function (index, item) {
    var Id=item.id;
    var JobId=item.JobId;
 var eachrow = "<tr>"
             + "<td>" + item.id + "</td>"
             + "<td>" + item.JobId + "</td>"
             + "<td>" + item.UserId + "</td>"
             + "<td>" + item.updatedAt + "</td>"
             + "<td>" + "<button onclick='myFunction("+JobId+","+Id+", '"accepted'")' class='btn btn-success btn-block'>Reject</button>" + "</td>"
             + "<td>" + "<button onclick='myFunction("+JobId+","+Id+", '"rejected'")' class='btn btn-success btn-block'>Reject</button>" + "</td>"
             + "</tr>";
 $('#tbody2').append(eachrow);
 });
},