繁体   English   中英

取消选择复选框并使用jQuery选择当前复选框

[英]deselect checkboxes and select the current one using jQuery

我需要一个愚蠢的问题的帮助/解决方案,因为这些复选框上都写有很多功能,对此非常抱歉。

我有复选框(至少三个)。 用户可以选择任何复选框,但应取消选择其他复选框,并通过在复选框上触发强制“单击”事件来取消选择其他复选框的条件,而不支持false。

如果上述说明不够清楚,请告诉我。

(注意:由于某些原因,我无法使用单选按钮)。

再次对这个愚蠢的问题感到抱歉,非常感谢您的帮助。

 <div class=" tiles"> <div class=" savingPack"> <input type="checkbox" name="savingspack" value="savingspack6" data-add-price-month="0" data-add-price-onetime="1500" data-other-price-divs=".addons"> <div class="tile"> <div class="tile-label">op - 1</div> </div> </div> <div class=" savingPack"> <input type="checkbox" name="savingspack" value="savingspack6" data-add-price-month="0" data-add-price-onetime="1500" data-other-price-divs=".addons"> <div class="tile"> <div class="tile-label">op - 2</div> </div> </div> <div class=" savingPack"> <input type="checkbox" name="savingspack" value="savingspack6" data-add-price-month="0" data-add-price-onetime="1500" data-other-price-divs=".addons"> <div class="tile"> <div class="tile-label">op - 3</div> </div> </div> </div> 

 $('.savingPack').on('click touchstart', function(event) { var checkbox, allCheckboxes; allCheckboxes = $('.savingPack').find('.tile'); checkbox = $(this).find('.tile'); checkbox.toggleClass('active'); var other = allCheckboxes.not(checkbox); if (other.hasClass('active')) { other.triggerHandler('click'); other.removeClass('active'); } else console.log(false); event.preventDefault(); }); 

尝试:

$('input[name="savingspack"]').on('click',function(){
   if(this.checked){
      $('.tiles input:checkbox').prop('checked',false);
      $(this).prop('checked',true);
   }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM