2016-09-27 100 views
0

我想製作一個表,其中行隱藏\顯示取決於如果選中標題中的複選框被選中或不。 我的腳本:JQuery隱藏顯示,如果複選框被選中

jQuery if checkbox is checked

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?

但不幸的是,他們不適合我,我不知道爲什麼。

+4

'show'是一種方法,用括號把它... =>'jQuery.show()' – Rayon

+4

它應該是' $(this).is(「:checked」)'better use'this.checked',你只需要使用'$(「#ShowPersonalDataList」)。 toogle(this.checked); });' – Satpal

+0

要檢查'checked'狀態,請使用'this.checked' ... – Rayon

回答

1

您有一個錯誤的選擇器來識別元素的狀態爲選中/未選中狀態。您可以使用this.checked得到檢查狀態,並把它作爲參數傳遞給.toggle()

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