php – 限制选中复选框(一次一个类别和一个子类别)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – 限制选中复选框(一次一个类别和一个子类别),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3249字,纯文字阅读大概需要5分钟。
内容图文
![php – 限制选中复选框(一次一个类别和一个子类别)](/upload/InfoBanner/zyjiaocheng/787/94eae3b9cdca48bdb0192397cdd3517f.jpg)
我的WordPress网站上有一个类别菜单列表,其代码如下所示:
<ul class="categorychecklist form-no-clear" data-wp-lists="list:product_cat" id="product_catchecklist">
<li id="product_cat-231"><label class="selectit"><input type="checkbox" id="in-product_cat-231" name="tax_input[product_cat][]" value="231"> Balls and sport items</label></li>
<li id="product_cat-204"><label class="selectit"><input type="checkbox" id="in-product_cat-204" name="tax_input[product_cat][]" value="204"> Beauty & Health</label>
<ul class="children">
<li id="product_cat-274"><label class="selectit"><input type="checkbox" id="in-product_cat-274" name="tax_input[product_cat][]" value="274"> Beauty</label>
<ul class="children">
<li id="product_cat-205"><label class="selectit"><input type="checkbox" id="in-product_cat-205" name="tax_input[product_cat][]" value="205"> Cosmetics</label></li>
<li id="product_cat-273"><label class="selectit"><input type="checkbox" id="in-product_cat-273" name="tax_input[product_cat][]" value="273"> Hair care</label></li>
<li id="product_cat-272"><label class="selectit"><input type="checkbox" id="in-product_cat-272" name="tax_input[product_cat][]" value="272"> Skin care</label></li>
</ul>
</li>
<li id="product_cat-214"><label class="selectit"><input type="checkbox" id="in-product_cat-214" name="tax_input[product_cat][]" value="214"> nutrition</label></li>
</ul>
</li>
<li class="popular-category" id="product_cat-206"><label class="selectit"><input type="checkbox" id="in-product_cat-206" name="tax_input[product_cat][]" value="206"> Clothing, Shoes & Jewelry</label>
<ul class="children">
<li class="popular-category" id="product_cat-223"><label class="selectit"><input type="checkbox" id="in-product_cat-223" name="tax_input[product_cat][]" value="223"> Accessories & Jewelry</label></li>
<li class="popular-category" id="product_cat-229"><label class="selectit"><input type="checkbox" id="in-product_cat-229" name="tax_input[product_cat][]" value="229"> Clothing</label></li>
<li class="popular-category" id="product_cat-208"><label class="selectit"><input type="checkbox" id="in-product_cat-208" name="tax_input[product_cat][]" value="208"> Handbags</label></li>
<li id="product_cat-217"><label class="selectit"><input type="checkbox" id="in-product_cat-217" name="tax_input[product_cat][]" value="217"> Shoes</label></li>
</ul>
</li>
它看起来像这样:http://prntscr.com/78hm0o
现在我希望用户可以一次选择一个主要类别和任何一个子类别(如果存在子类别).
到目前为止我尝试了什么:
$(':checkbox').on('change',function(){
var th = $(this), name = th.attr('name');
if(th.is(':checked')){
$(':checkbox[name="' + name + '"]').not(th).prop('checked',false);
}
});
由于每个类别和子类别的名称属性相同.因此,使用此代码一次只能选择一个.这不是我想要的.
解决方法:
试试这个:
$(':checkbox').on('click', function(){
// uncheck every checkbox
$(':checkbox').prop('checked', false);
// set initial variables
var checkbox = $(this), $ul, has_parent;
// loop
do {
// tick the selected checkbox
checkbox.prop('checked', true);
// get its closest UL
$ul = checkbox.closest('ul');
// check if selected category has a parent
has_parent = $ul.prev().is('.selectit');
// set the new checkbox to the one that belong to the parent
checkbox = $('> input', $ul.prev());
} while ( has_parent );
});
内容总结
以上是互联网集市为您收集整理的php – 限制选中复选框(一次一个类别和一个子类别)全部内容,希望文章能够帮你解决php – 限制选中复选框(一次一个类别和一个子类别)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。