admin管理员组文章数量:1320807
I have a list checkbox's
within a div which is not a form.
<div id="priceTree">
<div class="prices"><span class="left"><input type="checkbox" checked="checked" id="price_1"> Small </span><span class="right">5.00</span><span class="center"> </span></div>
<div class="prices"><span class="left"><input type="checkbox" checked="checked" id="price_2"> Medium </span><span class="right">10.00</span><span class="center"> </span></div>
<div class="prices"><span class="left"><input type="checkbox" checked="checked" id="price_3"> Large </span><span class="right">15.00</span><span class="center"> </span></div>
</div>
This list has injected elements so may not always have 3. How would I go about getting the id for only the checked checkboxs?
I have a list checkbox's
within a div which is not a form.
<div id="priceTree">
<div class="prices"><span class="left"><input type="checkbox" checked="checked" id="price_1"> Small </span><span class="right">5.00</span><span class="center"> </span></div>
<div class="prices"><span class="left"><input type="checkbox" checked="checked" id="price_2"> Medium </span><span class="right">10.00</span><span class="center"> </span></div>
<div class="prices"><span class="left"><input type="checkbox" checked="checked" id="price_3"> Large </span><span class="right">15.00</span><span class="center"> </span></div>
</div>
This list has injected elements so may not always have 3. How would I go about getting the id for only the checked checkboxs?
Share Improve this question asked Apr 18, 2014 at 8:08 maxummaxum 2,9154 gold badges36 silver badges49 bronze badges5 Answers
Reset to default 6Here is a vanilla JavaScript solution:
var checkedCbs = document.querySelectorAll('#priceTree input[type="checkbox"]:checked');
var ids = [];
for (var i = 0; i < checkedCbs.length; i++) ids.push(checkedCbs[i].id);
//return ids;
Demo
Try this
$("#priceTree input:checked").each(function(){
console.log(this.id);
});
Demo
If you want an array of checked elements' ids, use:
var result = $('#priceTree input:checked').map(function() {
return this.id;
}).get();
THE WORKING DEMO.
$('input[type=checkbox]:checked').attr('id');
To iterate over all checkboxes use each:
$('input[type=checkbox]:checked').each(function(){
console.log($(this).attr('id'));
});
Or,
$('#priceTree :checked').each(function(){
console.log($(this).attr('id'));
});
To get ids of all checkboxes checked within #priceTree
div:
$('#priceTree input:checked').each(function(){
alert($(this).attr('id'));
})
本文标签: javascriptGet checkbox value of items within divStack Overflow
版权声明:本文标题:javascript - Get checkbox value of items within div - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742089884a2420199.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论