【发布时间】:2020-02-08 06:14:06
【问题描述】:
我有带有 HTML 的复选框:
<input type="checkbox" class="selectMakeup" name="needsPartNumber">
可以有很多具有相同类的复选框。我想遍历每个选中的复选框。
$('.selectMakeup').each(function() {
console.log($(this).html())
console.log($(this).prop('checked'))
if ($(this).prop('checked')) {
//Do Stuff
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" class="selectMakeup" name="needsPartNumber">
console.log($(this).html())
返回
<input type="checkbox" name="needsPartNumber">
但是console.log($(this).prop('checked')) 返回undefined。
【问题讨论】:
-
第一个打印出
"",因为该元素没有innerHTML。第二个返回false,正如预期的那样。 jsfiddle.net/khrismuc/r64bdhqo - 遍历所有复选框:$('input[type=checkbox]:checked').each(...) -
如果第一个日志打印出您所说的打印内容,则表示您正在选择其父级。
-
没错,这也解释了为什么 prop 是未定义的。
-
此结果不能来自您显示的 HTML 和 JS。贴出真实代码。
-
在 Edge Chromium 中运行代码,我看到空白,然后是
false...
标签: javascript jquery html