我有一个如下所示的 HTML 表。我将从 jquery 访问该值。但是有一个问题
我的 HTML
<table class="table" id="examtbl">
<thead>
<tr>
<th>Ex No</th>
<th>Result</th>
</tr>
</thead>
<tbody>
<tr id="7500">
<td id="examNo">
<input class="form-control col-md-3 examNo" readonly="readonly" type="text" value="7500" />
</td>
<td>
<input class="form-control col-md-3" type="text" value="76" />
</td>
</tr>
<tr id="7600">
<td id="examNo">
<input class="form-control col-md-3 examNo" readonly="readonly" type="text" value="7600" />
</td>
<td>
<input class="form-control col-md-3" type="text" value="66" />
</td>
</tr>
</tbody>
</table>
我的脚本
$("#examtbl > tbody > tr").each(function () {
$(this).find('td').find("input").each(function () {
alert($(".examNo").val()); < -- It hits3 times but everytime it shows me 7500 only.
});
});
** 您能否给我一个如何获取这些值的解决方案。
最佳答案
问题是,你总是调用 $(".examNo")
...这意味着,他总是寻找第一个适合的项目...你必须使用 this
在每个内部循环中:)
$("#examtbl > tbody > tr").each(function () {
$(this).find('td').find("input").each(function () {
alert($(this).val());
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table" id="examtbl">
<thead>
<tr>
<th>Ex No</th>
<th>Result</th>
</tr>
</thead>
<tbody>
<tr id="7500">
<td id="examNo">
<input class="form-control col-md-3 examNo" readonly="readonly" type="text" value="7500" />
</td>
<td>
<input class="form-control col-md-3" type="text" value="76" />
</td>
</tr>
<tr id="7600">
<td id="examNo">
<input class="form-control col-md-3 examNo" readonly="readonly" type="text" value="7600" />
</td>
<td>
<input class="form-control col-md-3" type="text" value="66" />
</td>
</tr>
</tbody>
</table>
关于javascript - Html 表值未从 Jquery 获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40614412/