无法获得特定的表格单元格值

时间:2017-03-20 10:21:11

标签: jquery

我有一个代码可以在用户输入接受的数量时获取被拒绝的数量。但仅在特定的细胞中,我无法获得可接受的数量值。



$(document).on("keydown", "#accqty", function(e) {
  if (e.which == 9) {
    var row = $(this).closest("tr");
    var item = $(this).closest("tr").find('td:eq(2)').text();
    alert(item);
    var accepted = parseFloat($("#accqty").val());
    var received = $(this).closest("tr").find('td:eq(3)').text();
    alert("accepted   " + accepted);
    alert("received   " + received);
    var rejected = 0;
    if (accepted <= received) {
      rejected = received - accepted;
      row.find("td:eq(6) input[type='text']").val(rejected);
    } else {
      alert("Accepted qty can not be more than received; Please chcek!");
    }

    if (rejected > 0) {
      row.find("td:eq(6) input[type='text']").removeClass('tb1').addClass('reject');
    }
  }
});
&#13;
&#13;
&#13;

例如。我的桌子上有两排。对于第一行,我可以使用accqty id检索接受的数量的值。但对于第二行,如果第一行接受数量没有值或没有值,则接受的数量为空,或者如果存在,则从第一行获得接受的数量。请一些帮助。注意我的表将动态加载。

&#13;
&#13;
$.each(responseJson, function(key, value) {
  var rowNew = $("<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>");
  rowNew.children().eq(0).append(i);
  rowNew.children().eq(1).text(value['itemcode']);
  rowNew.children().eq(2).text(value['itemname']);
  rowNew.children().eq(3).text(value['receivedqty']);
  rowNew.children().eq(4).html('<input type="text"  id="inspdate"/>');
  rowNew.children().eq(5).html('<input type="text"  id="accqty" onkeypress="return isNumber(event)"/>');
  rowNew.children().eq(6).html('<input type="text"  id="rejqty" class="reject"/>');
  rowNew.children().eq(7).html('<input type="text"  id="rema"/>');
  rowNew.appendTo($tbody);
  i++;
});
&#13;
&#13;
&#13;

所以接受的数量总是从第一行开始

1 个答案:

答案 0 :(得分:0)

使用特定表上的更改输入来获取行值,如下所述

$(document).ready(function()  {   
	 $("#itemtable").on("change", "input", function() {
		 var row = $(this).closest("tr");
	      var received = parseFloat(row.find("td:eq(3)").text()); // get received qty
	      var accepted = parseFloat($(row).find("td:eq(5) input[type='text']").val()); //get accepted qty
	      
	      if (received < accepted){
	    	  alert("Accepted qty greater than received qty, Please check!");
	      } else {
	    	  var rejected = received - accepted;
	    	  row.find("td:eq(6) input[type='text']").val(isNaN(rejected) ? "" : rejected); 
	      }
	      
	      if (rejected > 0){
	    	  row.find("td:eq(6) input[type='text']").removeClass('tb1').addClass('reject');  
	      }
	 });
});