切换复选框和验证的类
Toggle class for CheckBoxes plus validations
我有很多复选框,95。它们都有相同的类,并以数组的形式命名。我需要将类从bg初级更改为bg成功或其他颜色。我还需要能够在最后说,"你错过了这个复选框"。这就是我现在所在的
<h3 class = "col-lg-12 text-center bg-warning "><a href="#">STEP 5 - Under the hood</a></h3>
<div id= "acc4">
<form>
<fieldset class="form-horizontal form-group bg-primary">
<input type="checkbox" class="box " name="checkBox[]" id="78">Check oil condition and level
</fieldset>
<fieldset class="form-horizontal form-group bg-primary">
<input type="checkbox" class="box " name="checkBox[]" id="79">Check coolant condition and level
</fieldset>
<fieldset class="form-horizontal form-group bg-primary">
<input type="checkbox" class="box " name="checkBox[]" id="80">Check trans fluid condition and level
</fieldset>
<fieldset class="form-horizontal form-group bg-primary">
<input type="checkbox" class="box " name="checkBox[]" id="81">Check power steering fluid condition and level
</fieldset>
<fieldset class="form-horizontal form-group bg-primary">
<input type="checkbox" class="box " name="checkBox[]" id="82">Check brake fluid condition and level
</fieldset>
<fieldset class="form-horizontal form-group bg-primary">
<input type="checkbox" class="box " name="checkBox[]" id="83">Fill washer fluid resevoir
</fieldset>
<fieldset class="form-horizontal form-group bg-primary">
<input type="checkbox" class="box " name="checkBox[]" id="84">Battery hold down
</fieldset>
<fieldset class="form-horizontal form-group bg-primary">
<input type="checkbox" class="box " name="checkBox[]" id="85">Check battery & charging system
</fieldset>
<fieldset class="form-horizontal form-group bg-primary">
<input type="checkbox" class="box " name="checkBox[]" id="86">Inspeect belts
</fieldset>
而我使用失败的Jquery是
$('input[type=checkbox]').on('change', function () {
$(this).toggleClass('form-horizontal form-group bg-warning', this.checked);
});
您应该使用.closest('fieldset')
,因为这是您想要应用类的地方。
$('input[type=checkbox]').on('change', function () {
$(this).closest('fieldset')
.removeClass('bg-primary') // remove the bg-primary once clicked
.toggleClass('bg-warning', !this.checked) // show warning when un-checked
.toggleClass('bg-success', this.checked); // show success when checked
});
要在最后找到未检查的,请使用
$('input[type=checkbox]').not(':checked') // add .closest('fieldset') if you need to target the containers again
使用closest()
获取父级和toggle
类bg-warning
。
$('input[type=checkbox]').on('change', function () {
$(this).closest('.form-horizontal.form-group').toggleClass('bg-warning', this.checked);
});
DEMO
最后我使用了add和remove类,以便从Bootstrap中获得更好的结果。借用我使用的第一个答案
$('input[type=checkbox]').on('change', function () {
$(this).closest('.form-horizontal.form-group').removeClass('bg-info', this.checked).addClass('bg-success', this.checked);});
这样可以获得更好的颜色效果,但如果框未选中,则无法切换回原来的颜色。
相关文章:
- jquery表单验证复选框错误显示
- HTML5/Javascript验证复选框提交URL重定向
- 验证复选框限制 JavaScript
- 在调用 ng-change 之前验证复选框
- JavaScript/jQuery:验证复选框和下拉列表配对
- “JQuery 验证”复选框不起作用
- 如何正确验证复选框值
- 使用JS验证复选框
- JavaScript验证复选框,当从选项列表中选择多个复选框时
- 使用form.submit验证复选框部分
- 验证复选框是否被选中——代码哪里出了问题
- Javascript表单验证复选框和单选不起作用
- 验证复选框-不检测它'选中
- 使用javascript验证复选框
- JQuery验证-复选框验证不发送所有值
- 验证复选框和输入
- 如何使用angularjs验证复选框(必选)
- Javascript按顺序验证复选框
- MVC4 Jquery验证复选框至少选中一个
- 验证复选框Jquery的多个复选或未选中