如何检查他人使用jQuery的时候我取消选中复选框(how to I uncheck boxes w

2019-11-01 18:17发布

我有这样的HTML -

<ol class="subproduct">
<li>
    <input type="checkbox" name="extra2" value="Multidispositivo" />
    <p class="checkbox"></p>
    <h3>test<span class="price"><span>+2</span>€</span></h3>
    <p>test words</p>
</li>
<li>
    <input type="checkbox" name="extra2" value="Multidispositivo" />
    <p class="checkbox"></p>
    <h3>test</h3>
    <p>test words</p>
</li>
<li>
    <input type="checkbox" name="extra2" value="15 min extra" />
    <p class="checkbox"></p>
    <h3>test/h3>
    <p>test words</p>
</li>
<li>
    <input type="checkbox" name="extra2" value="Multidispositivo" />
    <p class="checkbox"></p>
    <h3>test</h3>
    <p>test words</p>
</li>
</ol>

我需要一个类添加到李的“检查”复选框。 目前我使用这个

$(document).ready(function() {
  $('.subproduct input:checkbox').change(function(){
     if($(this).is(":checked")) {
         $(this).parent().toggleClass("checked");
     }
  });
});

但我需要它,以便永远只能一个复选框被用户选中,所以如果他们检查另一个盒子它消除了其他同类的

  • 这怎么可能?

  • Answer 1:

    看到这一点: 示例

    $(document).ready(function() {
     $('.subproduct input:checkbox').change(function(){
       $('.subproduct input:checkbox').not(this).attr("checked",false);
       $(".subproduct li").removeClass("checked");
       if($(this).is(":checked")) {
           $(this).parent().toggleClass("checked");
       }
     });
    });
    


    Answer 2:

    除去所有的类<li>的上chnage事件...用removeClass() ; ... ...然后如果选中toggleClass()即的<li>

    尝试这个

    $(document).ready(function() {
      $('.subproduct input:checkbox').change(function(){
       $(".subproduct li").removeClass("checked"); //<---here
        if($(this).is(":checked")) {
         $(this).parent().toggleClass("checked"); // and using addClass("checked") here is better .. 
        }
      });
     });
    


    Answer 3:

    试试这个http://jsfiddle.net/QHuV9/4/

     $(document).ready(function() {
      $('.subproduct input:checkbox').change(function(){
         if($(this).is(":checked")) {
             $(this).parent("li").addClass("checked");
         }
    
          else {
          $(this).parent("li").removeClass("checked");
          }
      });
    });
    

    我希望我把你的问题仪式



    Answer 4:

    最简单的办法是这样的:

    $(document).ready(function() {
      $('.subproduct input:checkbox').change(function(){
         if($(this).is(":checked")) {
             $(this).parent().siblings().removeClass('checked');
             $(this).parent().addClass("checked");
         }
      });
    });
    

    我会建议使用<input type="radio">使得行为将通过用户所理解。



    Answer 5:

    试试这个DEMO

    $(document).ready(function() {
      $('.subproduct input:checkbox').change(function(){
        if($(this).is(":checked")) {
           $(':checkbox').prop('checked',false);
           $('ol li').removeClass('checked');
           $(this).prop('checked',true);
           $(this).parent().addClass('checked');
        } else {
           $(this).parent().removeClass('checked');
        }
      });
    });
    


    文章来源: how to I uncheck boxes when checking others with jquery