[英]How to check for a checkbox to be checked?
I have 2 checkboxes. 我有2个复选框。 I need one, not both, but at least 1 to be checked .
我需要一个,而不是两个都需要检查 。 It is not a multiple selection but zero is not accepted.
它不是多选,但不接受零。 If one is checked, this line will work
jQuery('#wiz_menu.nav-tabs > .active').next('li').find('a').trigger('click');
如果选中,则此行将起作用
jQuery('#wiz_menu.nav-tabs > .active').next('li').find('a').trigger('click');
otherwise it should alert. 否则应该警惕。 The following makes it alerting all the time.
以下内容使其始终处于警报状态。
UPDATE UPDATE
I am not using the submit button or I would have used the validate plugin . 我没有使用提交按钮,或者我将使用validate插件 。 It is a normal button to go next in the wizard
<button type="button" class="btnNext">Next step</button>
这是向导中下一步的常规按钮
<button type="button" class="btnNext">Next step</button>
HTML HTML
<div class="form-group">
<label for="usp-category-8" class="usp-checkbox usp-cat usp-cat-0">
<input type="checkbox" name="usp-category[]" id="usp-category-8" value="8" data-required="true" class="usp-input usp-input-category">
Cultura
</label>
<label for="usp-category-7" class="usp-checkbox usp-cat usp-cat-0">
<input type="checkbox" name="usp-category[]" id="usp-category-7" value="7" data-required="true" class="usp-input usp-input-category">
Scienze
</label>
<input type="hidden" name="usp-category-required" value="1">
</div>
JS JS
jQuery('.btnNext').on("click", function(){
if(jQuery(".tab-pane").is("#step1")) {
var isChecked = false;
$('input[type=checkbox]').on("change", function () {
isChecked = true;
});
if ( isChecked ) {
jQuery('#wiz_menu.nav-tabs > .active').next('li').find('a').trigger('click');
} else {
alert( 'Please, check at least one checkbox!' );
}
}
});
With jQuery you can use is : 使用jQuery,您可以使用的是 :
$('#form').on('submit', function(e) { e.preventDefault(); if ($('input[name="hi"]').is(':checked')) { console.log('checked'); } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form id="form"> <input type="checkbox" name="hi" value="hi">Hallo<br> <input type="checkbox" name="gb" value="gb">Tschuss<br> <input type="submit" value="Submit"> </form>
Adapted to your code: 适应您的代码:
$('.btnNext').click(function(e) { e.preventDefault(); if ($('#usp-category-7').is(':checked') || $('#usp-category-8').is(':checked')) { console.log('at least one is checked'); } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="form-group"> <label for="usp-category-8" class="usp-checkbox usp-cat usp-cat-0"> <input type="checkbox" name="usp-category[]" id="usp-category-8" value="8" data-required="true" class="usp-input usp-input-category"> Cultura </label> <label for="usp-category-7" class="usp-checkbox usp-cat usp-cat-0"> <input type="checkbox" name="usp-category[]" id="usp-category-7" value="7" data-required="true" class="usp-input usp-input-category"> Scienze </label> <input type="hidden" name="usp-category-required" value="1"> <button class="btnNext">Next</button> </div>
Thanks to a comment, I personally resolved it like this, making them radio buttons. 多亏了评论,我亲自解决了这个问题,使它们成为单选按钮。 But I will accept the other answer as it is correct.
但我会接受另一个答案,因为它是正确的。
jQuery('.btnNext').on("click", function(){
if(jQuery(".tab-pane").is("#step1")) {
if($('input[type=radio').is(':checked')) {
jQuery('#wiz_menu.nav-tabs > .active').next('li').find('a').trigger('click');
} else {
alert( 'Please, check at least one checkbox!' );
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.