获取子元素中所有复选框的值
我想做的就是获取子元素中所有复选框的值,我看到了这个问题: 如何在 jQuery 中检索复选框值,但无法成功使其在子元素上工作
我的代码是这样的:
<ul id="menu3" class="side_menu expandfirst noaccordion">
<li>
<div class="refine_att"><a href="#" name="Expand/Collapse">Price</a></div>
<ul class="check">
<li><input onclick="updateTextArea()" id="_1" type="checkbox"><a href="#"> £0.00 - £9.99 </a></li>
<li><input onclick="updateTextArea()" id="_2" type="checkbox"><a href="#"> £10.00 - £99.99 </a></li>
<li><input onclick="updateTextArea()" id="_3" type="checkbox"><a href="#"> £100.00 - £249.99 </a></li>
<li><input onclick="updateTextArea()" id="_4" type="checkbox"><a href="#"> £250.00 - £499.99 </a></li>
<li><input onclick="updateTextArea()" id="_5" type="checkbox"><a href="#"> £500.00 - £999.99 </a></li>
</ul>
</div>
</li>
</ul>
也可能有多个“refine_att”列表,我在这里只显示了一个
function updateTextArea() {
var allVals = [];
$('#menu3').find(":checked").each(function() {
allVals.push($(this).val());
});
$('#t').val(allVals)
}
all im tryin to do is get the values of all checkboxes in child elements, i have seen this question: How to retrieve checkboxes values in jQuery but cant successfully get it working on child elements
My code is this:
<ul id="menu3" class="side_menu expandfirst noaccordion">
<li>
<div class="refine_att"><a href="#" name="Expand/Collapse">Price</a></div>
<ul class="check">
<li><input onclick="updateTextArea()" id="_1" type="checkbox"><a href="#"> £0.00 - £9.99 </a></li>
<li><input onclick="updateTextArea()" id="_2" type="checkbox"><a href="#"> £10.00 - £99.99 </a></li>
<li><input onclick="updateTextArea()" id="_3" type="checkbox"><a href="#"> £100.00 - £249.99 </a></li>
<li><input onclick="updateTextArea()" id="_4" type="checkbox"><a href="#"> £250.00 - £499.99 </a></li>
<li><input onclick="updateTextArea()" id="_5" type="checkbox"><a href="#"> £500.00 - £999.99 </a></li>
</ul>
</div>
</li>
</ul>
There could also be multiple 'refine_att' lists, ive shown just one here
function updateTextArea() {
var allVals = [];
$('#menu3').find(":checked").each(function() {
allVals.push($(this).val());
});
$('#t').val(allVals)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试以下操作:
JS Fiddle 演示。
You could try the following:
JS Fiddle demo.
难道您不应该检查复选框值的任何数据,而不是检查标签的内部 html 吗?另外,我会去掉 onclick,并对 ul 内的所有复选框进行 jquery 搜索。像(请注意,我添加了一个 id,并向复选框添加了值):
jQuery:
jQuery 对 id="ul_check" 元素内的复选框进行所有单击更改,以获取在id="ul_check" 元素,然后创建一个附加(“粘合”)字符串,并在值之间使用逗号“,”。如果每个复选框中没有 onClick,您就可以将 jQuery/javascript 与 HTML 分开,因此一切都变得更清晰且可监督。另外,jQuery 可以在 class="check" 上工作,但随后它会检查该类的其他元素,因此 ID 更适合这个 jQuery 建议。我假设您有一个元素,而不是文本区域?如果是文本区域,请使用:
strSelectedVals 将包含类似“0,10,100”的内容,具体取决于您选择的复选框。 (使用 PHP,如果您愿意,您可以执行explode(",", $POST["[...]"]) 来获取所有值,等等...)
Shouldn't you be checking any data of your checkbox values, rather than checking the inner html of your tags? Also, I would get rid of the onclick, and do a jquery search on ALL checkboxes inside the ul. Like (notice that I added an id, and added values to the checkboxes):
jQuery:
The jQuery makes all click-changes on checkboxes within the element with id="ul_check", to get all elements of type checkboxes that are checked WITHIN the id="ul_check" element, then create an appended ('glued') string with a comma ',' in between the values. Without the onClick in each checkbox, you are separating jQuery/javascript from HTML, so it all becomes cleaner and overseeable. Also, the jQuery could work on class="check", but it would then check other elements with that class, so ID is better for this jQuery suggestion. I am assuming that you have a element, instead of a textarea? If textarea, use:
strSelectedVals will contain something like "0,10,100" depending on which checkboxes you selected. (With PHP, you can, if you wish, then do an explode(",", $POST["[...]"]) to get all values, etc...)