1

我正在寻找如何在标题上添加一个复选框的方法,该复选框支持选中或取消选中我的 girdview 的所有复选框列。

    <table class="grid">
    <th><input type="checkbox" name="chkall"/></th>
    <th>Name</th>
     <tr>
         <td>       
            <input type="checkbox" id="chkItem_1"/>
         </td>
         <td>
             Category 1
         </td> 
     </tr>
     <tr>
         <td>       
            <input type="checkbox" id="chkItem_2"/>
         </td>
          <td>
             Category 2
         </td>
     </tr>
</table>  
4

2 回答 2

9
column.For(x => Html.CheckBox("mycheckbox", new { @class = "foo" }))
    .DoNotEncode()
    .Header("<th><input type=\"checkbox\" id="chkHeader" /></th>");

然后您可以使用 jquery 来处理标题复选框的更改事件并选中/取消选中所有其他:

$(function() {
    $('#chkHeader').change(function() {
        if ($(this).is(':checked')) {
            $('.foo').attr('checked', 'checked');
        } else {
            $('.foo').removeAttr('checked');
        }
    });
});
于 2010-11-25T07:28:51.963 回答
5

以下对我有用:

column.For(x => Html.CheckBox('chkBox', x.Published)).Named('Published');
于 2011-07-28T12:02:02.357 回答