Javascript 在主干中查找所有选中的复选框
我弹出了一个引导模式,允许用户选择一些复选框。然后在我看来,我想循环检查每一个,看看它们是否被检查过 这是我的模式代码:Javascript 在主干中查找所有选中的复选框,javascript,jquery,twitter-bootstrap,backbone.js,coffeescript,Javascript,Jquery,Twitter Bootstrap,Backbone.js,Coffeescript,我弹出了一个引导模式,允许用户选择一些复选框。然后在我看来,我想循环检查每一个,看看它们是否被检查过 这是我的模式代码: <div class="modal fade" id="ethnicityModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-
<div class="modal fade" id="ethnicityModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4 class="modal-title" id="myModalLabel">Ethnicity Targeting</h4>
</div>
<div class="modal-body">
<h5>Select ethnicities that should be included:</h5>
<input type="checkbox" name="caucasian"><label for="#caucasian"> Caucasian</label>
<br>
<input type="checkbox" name="black"><label for="#black"> African American</label>
<br>
<input type="checkbox" name="hispanic"><label for="#hispanic"> Hispanic/Latino</label>
<br>
<input type="checkbox" name="middleEastern"><label for="#middleEastern"> Middle Eastern</label>
<br>
<input type="checkbox" name="pacific"><label for="#pacific"> Pacific Islander</label>
<br>
<input type="checkbox" name="nativeAmerican"><label for="#nativeAmerican"> Native American/Alaskan</label>
<br>
<input type="checkbox" name="other"><label for="#other"> Other</label>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary saveEthnicity" data-dismiss="modal">Save</button>
</div>
</div>
</div>
</div>
因此e.currentTarget.parentElement.parentNode.children[1].children
返回子元素数组。但我似乎无法在它们之间循环。我试过了
e.currentTarget.parentElement.parentNode.children[1].children.each (child) ->
console.log child
但这是一个错误。你知道如何获取每个输入,以便对其执行
.checked()
操作吗?如果主干视图正确表示模式窗口,那么你只需使用jQuery搜索所有复选框即可:
var $checkedBoxes = this.$('input:checked');
从:
每个视图都有一个$
函数,该函数在视图的元素中运行范围内的查询
如果视图未直接表示模式,则可以向上搜索DOM树,直到找到最近的模式:
var $checkedBoxes = $(e.currentTarget).closest('modal').find('input:checked')
children
属性不是数组,而是HTMLCollection
,请尝试以下操作:
children = [].slice.apply(e.currentTarget.parentElement.parentNode.children[1].children)
children.forEach (child) -> console.log(child)
不完全是这样-我实际上在视图的模板中有许多模态,其中许多使用复选框。所以我想我可以给每一个集合添加一个唯一的类,然后从那个集合开始。@TomHammond您肯定应该使用一些东西来识别按钮提交的模式。@TomHammond我给了您一个替代方法。啊,这很有意义。谢谢你的帮助!
children = [].slice.apply(e.currentTarget.parentElement.parentNode.children[1].children)
children.forEach (child) -> console.log(child)