如何选择全部或取消选中全部复选框? [英] How to Select All or Deselect All checkboxes?

查看:104
本文介绍了如何选择全部或取消选中全部复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



< asp:gridview>我有

Hi,
in <asp:gridview> i have

<asp:TemplateField>
  <ItemTemplate>
    <input type="checkbox" id="selectProduct" name="selectProductName" value="<%# Eval("Column") %>" />
  </ItemTemplate>
</asp:TemplateField>





我想为Select添加两个按钮All and Deselect All,like that



I want to add two buttons for Select All and Deselect All, like that

<asp:Button ID="selectAll" Text="Select All" runat="server" /></td>





我在这个和其他网站上尝试了很多解决方案,但是我有选择和取消选择复选框的问题因为,我有< input type =checkbox而不是< asp:chceckbox。我怎么能解决这个问题?如果我创建这样的复选框:< asp:chceckbox ...我不知道,如何添加复选复选框的值(<%#Eval(Column)%>)?



谢谢



I try many solutions on this and other site, but i have problem with select and deselect checkboxes because, i have < input type="checkbox" and not a <asp:chceckbox . How i can solve this problem? If i create checkboxes like this: <asp:chceckbox... i dont know, how i can add value of checked checkbox (<%# Eval("Column") %>)?

Thanks

推荐答案

你可以像下面那样绑定

You can bind like below
<asp:checkbox id="selectProduct" runat="server" checked='<%#Eval("column")%>' ></asp:checkbox>



如果 Eval(column)不返回 bool 值。例如


This will fail if Eval("column") not return boolvalue. for example

Checked='<%# Convert.ToBoolean(Eval("column")) %>' 



将转换为bool值并将其设置为checked属性。如果您没有有效的布尔字符串值,则上面也会失败 Eval(column)


您可以尝试:

You may try with:
<html>
<body>

<p id="demo"></p>
<table id="dataTable" border="1">
    <tr>
        <th>
            <INPUT type="checkbox" onchange="checkAll(this)" name="chk[]" />
        </th>
        <th>Make</th>
        <th>Model</th>
        <th>Description</th>
        <th>Start Year</th>
        <th>End Year</th>
    </tr>
    <tr>
        <td>
            <INPUT type="checkbox"  name="chk1[]" />
        </td>
        <td>2008</td>
        <td>1200p</td>
        <td>User Interface</td>
        <td>2006</td>
        <td>2020</td>
    </tr>
 <tr>
        <td>
            <INPUT type="checkbox"  name="chk1[]" />
        </td>
        <td>2003</td>
        <td>1200oop</td>
        <td>User Interface</td>
        <td>2003</td>
        <td>2025</td>
    </tr>
</table>

<script>
function checkAll(source) {
     var checkboxes = new Array();
     checkboxes = document.getElementsByTagName('input');
     
     for (var i = 0; i < checkboxes.length; i++) {
         if (checkboxes[i].type == 'checkbox') {
             checkboxes[i].checked = source.checked;
         }
         
     }
 }
</script>
</body>
</html>


function checkAll() {
     var checkboxes = new Array();
     checkboxes = document.getElementsByTagName('input');

     for (var i = 0; i < checkboxes.length; i++) {
         if (checkboxes[i].type == 'checkbox') {
             checkboxes[i].setAttribute('checked', true)
         }
     }
 }







<INPUT type="checkbox" onchange="checkAll()" name="chk[]"/>





你可以用javascript函数来做尝试一下。



问候,

Praveen Nelge



U can do it using javascript function try it out.

Regards,
Praveen Nelge


这篇关于如何选择全部或取消选中全部复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆