我想检查是否选中了复选框。 我有以下代码:
<div class="checkbox">
@Html.HiddenFor(model => model.VehicleTypeSelected)
@Html.ValidationMessageFor(model => model.VehicleTypeSelected)
<br>
@foreach (VehicleType vt in ViewBag.VehicleTypes)
{
<label><input type="checkbox" value="@vt.VehicleTypeID" name="VehicleTypeIds" class="vehicle-type-selector" /> @vt.Name
<br></label>
}
我想使用这种方法:
$('#checkBoxForm :checkbox').click(function() {
var $this = $(this);
if ($this.is(':checked')) {
// the checkbox was checked
} else {
// the checkbox was unchecked
}
});
如果选中复选框(isUniversal),我想禁用所有复选框(vehicleTypes)
答案 0 :(得分:1)
您可以使用以下方法检查是否检查了任何车辆类型:
$(function(){
var isAnyVehicleTypeChecked = $(".vehicle-type-selector").is(":checked").length > 0
});
此外,使用以下
$(function(){
var checkedVehicleTypes = $(".vehicle-type-selector").is(":checked");
});
您可以选中所有具有类型车辆类型选择器的复选框,然后选中它们。
答案 1 :(得分:0)
使用attr()
函数
尝试(假设isUniversal
是一个类):
$(".isUniversal").on('click',function(){
if ($("this").attr('checked') == 'checked') {
$('.vehicle-type-selector').removeAttr('checked').attr('disabled','disabled');
}
})
或
$(".isUniversal").on('click',function(){
if( $('this').is(":checked") ){
$('.vehicle-type-selector').removeAttr('checked').attr('disabled','disabled');
}
})
答案 2 :(得分:0)
if( $('.vehicle-type-selector').is(":checked") ){
// Do something
}