簡體   English   中英

jQuery隱藏\\顯示是否選中復選框

[英]JQuery hide\show if checkbox is checked

我想制作一個表格,其中是否隱藏\\顯示行,具體取決於是否選中了標題中的復選框。 我的劇本:

<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>

我嘗試過以下解決方案:

jQuery如果復選框已選中

jQuery,復選框和.is(“:checked”)

如何檢查jQuery中是否已選中復選框?

但不幸的是,它們對我不起作用,我不知道為什么。

您使用錯誤的選擇器將元素狀態標識為已選中/未選中。 您可以使用this.checked獲取檢查狀態並將其用作.toggle()參數:

$("#ShowPersonalDataList").change(function () {
   $(".PersonalDataInset").toggle(this.checked);
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM