这是我的桌子的底座。它可以有更多的行,name属性中的最后一个数字表示行号。我希望获得最后一行中任何输入的name属性。哪个输入并不重要,因为这样我就会拆分字符串,只保留最后一个数字。
我怎样才能做到这一点?谢谢!
<table id="tableNames">
<tbody>
<tr>
<td>
<input type="text" name="contact1_1"/>
<input type="text" name="contact2_1"/>
<input type="text" name="contact3_1"/>
</td>
<td>
<input type="text" name="phone1_1"/>
<input type="text" name="phone2_1"/>
<input type="text" name="phone3_1"/>
</td>
</tr>
</tbody>
</table>发布于 2017-05-22 18:48:36
带有split()和pop()的split()选择器
console.log( $("#tableNames tbody tr:last input:last").attr("name").split("_").pop())<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="tableNames">
<tbody>
<tr>
<td>
<input type="text" name="contact1_1"/>
<input type="text" name="contact2_1"/>
<input type="text" name="contact3_1"/>
</td>
<td>
<input type="text" name="phone1_1"/>
<input type="text" name="phone2_1"/>
<input type="text" name="phone3_1"/>
</td>
</tr>
</tbody>
</table>
发布于 2017-05-22 18:48:45
$(document).ready(function() {
alert($("#tableNames").find("tr:last").find("input").attr("name"));
})或者更短的版本:
$(document).ready(function() {
alert($("#tableNames tr:last input").attr("name"));
})下面是获取最后一行输入的名称的小提琴:https://jsfiddle.net/L7k2w6Ls/2/
发布于 2017-05-22 18:53:41
$('#tableNames tr:last input').map(function(){return $(this).attr('name');}).get();https://stackoverflow.com/questions/44119932
复制相似问题