JQuery隐藏\显示,如果复选框被选中
问题描述:
我想制作一个表,其中行隐藏\显示取决于如果选中标题中的复选框被选中或不。 我的脚本:JQuery隐藏显示,如果复选框被选中
jQuery, checkboxes and .is(":checked")
0123:<script contextmenu="text/javascript">
$(document).ready(function() {
$("#ShowPersonalDataList").change(function() {
if($(this).is("checked")){
$(".PersonalDataInset").Show;
return
} else
$(".PersonalDataInset").hide;
});
});
我的HTML:从
<div id="CheckBoxTables">
<table class="CBTable">
<tr>
<th>
@Html.LabelFor(m => m.ColumnsNeeded.PersonalDataPartBool)
@Html.CheckBoxFor(m => m.ColumnsNeeded.PersonalDataPartBool, new { id = "ShowPersonalDataList" })
</th>
</tr>
<tr>
<td class="PersonalDataInset">
@Html.LabelFor(m => m.ColumnsNeeded.FirstNameBool)
@Html.CheckBoxFor(m => m.ColumnsNeeded.FirstNameBool)
</td>
</tr>
<tr>
<td class="PersonalDataInset">
@Html.LabelFor(m => m.ColumnsNeeded.LastNameBool)
@Html.CheckBoxFor(m => m.ColumnsNeeded.LastNameBool)
</td>
</tr>
<tr>
<td class="PersonalDataInset">
@Html.LabelFor(m => m.ColumnsNeeded.AppointmentBool)
@Html.CheckBoxFor(m => m.ColumnsNeeded.AppointmentBool)
</td>
</tr>
<tr>
<td class="PersonalDataInset">
@Html.LabelFor(m => m.ColumnsNeeded.DivisionBool)
@Html.CheckBoxFor(m => m.ColumnsNeeded.DivisionBool)
</td>
</tr>
</table>
</div>
我试图解决方案
How to check whether a checkbox is checked in jQuery?
但不幸的是,他们不适合我,我不知道为什么。
答
您有一个错误的选择器来识别元素的状态为选中/未选中状态。您可以使用this.checked
得到检查状态,并把它作为参数传递给.toggle()
:
$("#ShowPersonalDataList").change(function() {
$(".PersonalDataInset").toggle(this.checked);
});
'show'是一种方法,用括号把它... =>'jQuery.show()' – Rayon
它应该是' $(this).is(“:checked”)'better use'this.checked',你只需要使用'$(“#ShowPersonalDataList”)。 toogle(this.checked); });' – Satpal
要检查'checked'状态,请使用'this.checked' ... – Rayon