javascript - 无法测试每个循环中检查的输入

标签 javascript jquery html checkbox

<分区>

我有一个简单的输入复选框元素对象,我想在每个循环中检查哪些被选中并执行哪些操作。复选框在一个 tr 中在几个td在表格中。

这是我的代码:

$('table.occupancy-table').on('change', '.minimum_stay input', function() {
    var minimumStayInputs = $(this).closest('tr').find('input');
    $(minimumStayInputs).each(function(){
        if(this.prop('checked')) {
        //do stuff
        }
    }
}

如果我 console.log this在每个循环中,我得到如下数据:

<input id="R11520" type="checkbox" name="minimum_stay[152][1442786400]" value="checked"> <input id="R11521" type="checkbox" name="minimum_stay[152][1442872800]" value="checked"> ...

但我总是得到一个错误Uncaught TypeError: this.prop is not a function来自 this.prop('checked') .我也试过.is(":checked")结果相同。

任何想法可能是什么原因?

如果您需要示例 html 或我应该创建 fiddle ,请告诉我。

最佳答案

循环内的

this 是 native DOM 元素,从 jQuery 展开。

还得再包一次

$('table.occupancy-table').on('change', '.minimum_stay input', function() {
    var minimumStayInputs = $(this).closest('tr').find('input');
    $(minimumStayInputs).each(function(){
        if ( $(this).prop('checked') ) {
        }
    });
});

你也可以只使用原生的 this.checked,或者如果你想对它们进行计数

$(this).closest('tr').find('input:checked').length

关于javascript - 无法测试每个循环中检查的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29902140/

相关文章:

jQuery AJAX 响应类型

jquery - div {位置 :absolute} renders at different locations on different width screens

php - PHP IF 语句中的 echo 变量和 HTML

html - 为什么背景颜色不起作用? - CSS

html - 引导轮播的视差效果在 Firefox 中不起作用

javascript - 查找对象中的 2 个最高值 - Javascript

javascript - Passport.js 和 MongoStore 错误 "Connection strategy not found at MongoStore"

javascript - 返回时恢复页面内容

javascript - 将相同的模型变量绑定(bind)到单选按钮和输入文本

javascript - 没有setTimeout的CSS连续旋转