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 srs-sip外部服务 webrtc支持H265格式
  • ¥15 在使用abaqus软件中,继承到assembly里的surfaces怎么使用python批量调动
  • ¥15 大一C语言期末考试,求帮助🙏🙏
  • ¥15 ch340驱动未分配COM
  • ¥15 Converting circular structure to JSON
  • ¥30 Hyper-v虚拟机相关问题,求解答。
  • ¥15 TSM320F2808PZA芯片 Bootloader
  • ¥45 谷歌浏览器出现开发者工具无法显示已创建的,但您可以调试已部署的代码。 状态代码 404, net::ERR HTTP RESPONSE CODE FAILURE
  • ¥15 如何解决蓝牙通话音频突发失真问题
  • ¥15 安装opengauss数据库报错