如何通过单击按钮获取ajax响应

时间:2017-04-21 06:50:00

标签: javascript jquery

我从ajax响应中获取表数据为json。
有些json数据没有显示但是我希望点击按钮用于其他目的。我怎么能得到它?请帮助我。

 function leaveTable() {
     for (var i = 0; i < leaveList.length; i++) {
         var tab = '<tr id="' + i + '"><td>' + (i + 1) + '</td><td class="appliedOn">' + leaveList[i].appliedOn + '</td><td class="levType" >' + leaveList[i].levType + '</td><td class="leaveOn" >' + leaveList[i].leaveOn + '</td><td class="duration">' + leaveList[i].duration + '</td><td class="status">' + leaveList[i].status + '</td><td class="approvedOn">' + leaveList[i].approvedOn + '</td><td class="approvedBy">' + leaveList[i].approvedBy + '</td><td><i class="btn dltLev fa fa-times" onclick="cancelLeave(this)" data-dismiss="modal"  value="Cancelled"></i></td><tr>';

         $('#levListTable').append(tab)
     }
 }
来自ajax响应的

我想要leaveTypeId并将其传递给sendCancelReq()函数 完整代码:https://jsfiddle.net/tytzuckz/18/

2 个答案:

答案 0 :(得分:1)

确切地知道你想要什么是很复杂的。我希望能帮到你:

第一个,我会更改,在您的html代码df['Coefficient'] = [0.1,0.2,0.1,0.5,0.2,0.3,0.2,0.6,0.9,0.8,0.5,0.3,0.5,0.8,0.4,0.1,0.2,0.5,0.9,0.7,0.2,0.5,0.5,0.2,0.8,0.3,0.6,0.5,0.2,0.2,0.4,0.1,0.3,0.9,0.8,0.2,0.5,0.6,0.5] 中生成JavaScript事件。我认为,在创建新的dom元素后添加事件时,它更清晰可读。例如:

var tab = ...

然后,您可以更清楚地发送您的必要信息,例如:

而不是最后一个代码

var tab = $('<tr id="' + i + '">' + 
  '<td>' + (i + 1) + '</td>' + 
  '<td class="appliedOn">' + leaveList[i].appliedOn + '</td>' + 
  '<td class="levType" >' + leaveList[i].levType + '</td>' + 
  '<td class="leaveOn" >' + leaveList[i].leaveOn + '</td>' + 
  '<td class="duration">' + leaveList[i].duration + '</td>' + 
  '<td class="status">' + leaveList[i].status + '</td>' + 
  '<td class="approvedOn">' + leaveList[i].approvedOn + '</td>' + 
  '<td class="approvedBy">' + leaveList[i].approvedBy + '</td>' + 
  '<td><i class="btn dltLev fa fa-times" data-dismiss="modal"  value="Cancelled"></i></td>' + 
  '<tr>');
$(tab).find('.btn.dltLev').click(function () { cancelLeave(this); });

你可以写

$(tab).find('.btn.dltLev').click(function () { cancelLeave(this); });

并将您的方法$(tab).find('.btn.dltLev').click(function () { cancelLeave(this, leaveList[i].leaveTypeId); }); 扩展为:

cancelLeave

}

答案 1 :(得分:0)

得到解决方案
请检查:https://jsfiddle.net/tytzuckz/19/

 function cancelLeave(elem) {
    var levTypeId = $(elem).attr('id')
    var id = $(elem).closest('tr').attr('id')
    $('.currentLevTypeId').val(levTypeId);
     $("#cancelLeave").modal("show");
     $('.sendCancelReq').val(id);

   }
   function sendCancelReq() {

  var a= $('.currentLevTypeId').val();
   alert(a)
 }