使用输入循环html表以获取值
我有一张像这样的桌子使用输入循环html表以获取值,html,jquery,Html,Jquery,我有一张像这样的桌子 <table id="tableScanItems" class="table"> <th>Scan Item</th> <th>Inactive</th> <tr> <td><input type='text' id='input1' class='form-control'/>&l
<table id="tableScanItems" class="table">
<th>Scan Item</th>
<th>Inactive</th>
<tr>
<td><input type='text' id='input1' class='form-control'/></td>
<td><input type='checkbox' id='checkbox1' class='form-control'/></td>
</tr>
<tr>
<td><input type='text' id='input2' class='form-control'/></td>
<td><input type='checkbox' id='checkbox2' class='form-control'/></td>
</tr>
<tr>
<td><input type='text' id='input3' class='form-control'/></td>
<td><input type='checkbox' id='checkbox3' class='form-control'/></td>
</tr>
</table>
我需要循环html表以获取输入类型=文本和输入类型=复选框的值
JS确实正确地循环并获取输入类型=文本值,但不获取输入类型=复选框值。是否需要在.each中指定不同的参数以查看复选框的值
瑞安
编辑
如果我这样做,我就看不到用户输入的值
$(this).closest('tr').find("input").each(function() {
alert(this.value)
});
选中一些复选框并点击提交。您将在div中看到复选框的值
$(文档).ready(函数(){
$('#btnsupmit')。单击(函数(){
var result=$('input[type=“checkbox”]:checked')
如果(result.length>0){
var resultString=result.length+“已选中”
结果。每个(函数(){
resultString+=$(this.val()+“
”;
});
$('#divCheckboxValues').html(resultString);
}
否则{
$('#divCheckboxValues').html(“未选中复选框”);
}
});
});代码>
扫描项目
不活跃的
您可以像下面这样循环。您需要以不同的方式对待复选框以获取其值:
$("#tableScanItems tr").each(function (){
$(this).closest('tr').find("input").each(function() {
var elem = $(this);
if (elem.is(':checkbox')) {
console.log( !!elem.attr("checked"));
} else {
console.log(elem.val());
}
});
});
此处的工作小提琴:无法复制-此小提琴打印出所有6个输入的ID。谢谢@PetrHejda我感谢你的时间。我正在尝试获取输入的值。在这种情况下,您可以使用jQueryval()
方法-。。。因此,与其调用$(this.attr('id')
,不如调用$(this.val()
@PetrHejda,此.value
的工作原理是这样的,因为此
引用函数循环中的元素本身<代码>但是,最近('tr')
是没有意义的。您的问题是没有触发此代码的事件。
$("#tableScanItems tr").each(function (){
$(this).closest('tr').find("input").each(function() {
var elem = $(this);
if (elem.is(':checkbox')) {
console.log( !!elem.attr("checked"));
} else {
console.log(elem.val());
}
});
});