jq获取动态添加的行 并查找点击行同胞元素中的input值 遍历table中td下元素的值

1.非动态添加的行 获取同胞中的input输入值
$("op-submit").click(function(event){ var text1 = $(event.currentTarget).parent().prev().find("input").val(); var text2 = $(event.currentTarget).parent().prev().prev().find("input").val(); alert(text1); alert(text2); });

2.动态添加的行 获取同胞中的input输入值(非动态的也可以取到)
$(document).on("click",".op-submit",function(){
var addNum = $(this).parent().prev().prev().find("input").val();
var reason = $(this).parent().prev().find("input").val();
});

3.删除动态添加的行
$(document).on("click",".op-cancel",function(){
$(this).parent().parent().remove();
});



----------------------------------------jq获取table中的td内元素的值-------------------------------------------
由于一个表格里面不仅要显示数据,也可能接受用户输入+显示+选择混用,所以我们不能一概的.text()或find('input')去获得所有元素值。
原文地址:http://www.jb51.net/article/84629.htm

html代码:


<tbody >
<tr>
<td align="center"><input type="text" class="input-s input-w input-hs"></td>
<td align="center"><input type="text" class="input-s input-w input-hs"></td>
<td align="center"><input type="text" class="input-s input-w input-hs"></td>
<td align="center"><a class="" onclick="history_income_del(this);" href="###">删除</a></td>
</tr>
<tr>
<td align="center"><input type="text" class="input-s input-w input-hs"></td>
<td align="center"><input type="text" class="input-s input-w input-hs"></td>
<td align="center"><input type="text" class="input-s input-w input-hs"></td>
<td align="center"><a class="" href="###">删除</a></td>
</tr>
<tr>
<td align="center"><input type="text" class="input-s input-w input-hs"></td>
<td align="center"><input type="text" class="input-s input-w input-hs"></td>
<td align="center"><input type="text" class="input-s input-w input-hs"></td>
<td align="center"><a class="" href="###">删除</a></td>
</tr>
</tbody>

方法一:


var trList = $("#history_income_list").children("tr")
  for (var i=0;i<trList.length;i++) {
    var tdArr = trList.eq(i).find("td");
    var history_income_type = tdArr.eq(0).find("input").val();//收入类别
    var history_income_money = tdArr.eq(1).find("input").val();//收入金额
    var history_income_remark = tdArr.eq(2).find("input").val();//  备注
    
    alert(history_income_type);
    alert(history_income_money);
    alert(history_income_remark);
  }

方法二:


$("#history_income_list").find("tr").each(function(){
    var tdArr = $(this).children();
    var history_income_type = tdArr.eq(0).find("input").val();//收入类别
    var history_income_money = tdArr.eq(1).find("input").val();//收入金额
    var history_income_remark = tdArr.eq(2).find("input").val();//  备注
    
    alert(history_income_type);
    alert(history_income_money);
    alert(history_income_remark);
    
    
  });