dongyu1918 2018-07-19 09:04
浏览 41
已采纳

如何取消选中嵌套复选框并在jquery中保留当前单击的复选框?

I have nested checkboxes wrapped in a ul and li in WordPress. The html is written below.

<ul class="woof_list woof_list_checkbox">
    <li>
        <label class="woof_checkbox_label " for="woof_uncheck">Parent Category 1</label>

        <ul class="woof_childs_list">

            <li class="woof_childs_list_li">
                <label class="woof_checkbox_label " for="woof_uncheck">Sub Category 1</label>

                <ul class="woof_childs_list">

                    <li>
                        <input type="checkbox"  class="woof_checkbox_term" data-tax="product_cat" name="" data-term-id="654" value="654">
                        <label class="woof_checkbox_label " for="">Product 1</label>
                    </li>

                    <li>
                        <input type="checkbox"  class="woof_checkbox_term" data-tax="product_cat" name="" data-term-id="644" value="644">
                        <label class="woof_checkbox_label " for="">Product 2</label>
                    </li>

                </ul>

            </li>

            <li class="woof_childs_list_li">
                <label class="woof_checkbox_label " for="woof_uncheck">Sub Category 2</label>

                <ul class="woof_childs_list">

                    <li>
                        <input type="checkbox"  class="woof_checkbox_term" data-tax="product_cat" name="" data-term-id="541" value="541">
                        <label class="woof_checkbox_label " for="">Product 3</label>
                    </li>

                    <li>
                        <input type="checkbox"  class="woof_checkbox_term" data-tax="product_cat" name="" data-term-id="542" value="542">
                        <label class="woof_checkbox_label " for="">Product 4</label>
                    </li>

                </ul>

            </li>


        </ul>

    </li>
</ul>
  • Parent Category 1
    • Sub Category 1
      • [checkbox] Product 1
      • [checkbox] Product 2
    • Sub Category 2
      • [checkbox] Product 3
      • [checkbox] Product 4

The default implementation of the html above is when I click any of the product labels, the checkbox beside it will be checked

Do you know how can I uncheck all of the checkboxes when I click a Product label but still check the clicked label?

For example: Product 4 checkbox is currently checked and when I click the label for Product 1, I want to uncheck Product 4 checkbox but Product checkbox will now be checked.

Do you have any idea how to do this? Thanks

My current jquery code is:

  $('.woof_list woof_list_checkbox > li > .woof_childs_list > .woof_childs_list_li > .woof_childs_list > li > woof_checkbox_label ').click(function(){

   $('.woof_checkbox_term').prop('checked', false);

   $(this).find('.woof_checkbox_term').trigger('click');

  });
  • 写回答

1条回答 默认 最新

  • dtxob80644 2018-07-19 10:06
    关注
      Try this **JQUERY** Code
    

    $(function(){
        $('.woof_checkbox_term').on('click',function(e){
            $('.woof_checkbox_term').prop('checked', false);
            $(e.target).prop('checked',true);
        });
    })
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <ul class="woof_list woof_list_checkbox">
        <li>
            <label class="woof_checkbox_label " for="woof_uncheck">Parent Category 1</label>
    
            <ul class="woof_childs_list">
    
                <li class="woof_childs_list_li">
                    <label class="woof_checkbox_label " for="woof_uncheck">Sub Category 1</label>
    
                    <ul class="woof_childs_list">
    
                        <li>
                            <input type="checkbox" id="p1" class="woof_checkbox_term" data-tax="product_cat" name="" data-term-id="654" value="654">
                            <label class="woof_checkbox_label " for="p1">Product 1</label>
                        </li>
    
                        <li>
                            <input type="checkbox" id="p2" class="woof_checkbox_term" data-tax="product_cat" name="" data-term-id="644" value="644">
                            <label class="woof_checkbox_label " for="p2">Product 2</label>
                        </li>
    
                    </ul>
    
                </li>
    
                <li class="woof_childs_list_li">
                    <label class="woof_checkbox_label " for="woof_uncheck">Sub Category 2</label>
    
                    <ul class="woof_childs_list">
    
                        <li>
                            <input type="checkbox" id="p3" class="woof_checkbox_term" data-tax="product_cat" name="" data-term-id="541" value="541">
                            <label class="woof_checkbox_label " for="p3">Product 3</label>
                        </li>
    
                        <li>
                            <input type="checkbox" id="p4" class="woof_checkbox_term" data-tax="product_cat" name="" data-term-id="542" value="542">
                            <label class="woof_checkbox_label " for="p4">Product 4</label>
                        </li>
    
                    </ul>
    
                </li>
    
    
            </ul>
    
        </li>
    </ul>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?