【问题标题】:Uncheck asp.net checkboxlist values using javascript or jquery使用 javascript 或 jquery 取消选中 asp.net checkboxlist 值
【发布时间】:2013-05-22 02:49:06
【问题描述】:

我有一个 asp.net 复选框列表,并且 checkboxlist 的值是从数据库绑定的。我的要求是取消选中按钮单击上的复选框。有人可以建议我如何使用 javascript 或 jquery 取消选中复选框。谢谢

【问题讨论】:

  • 它们长什么样?您要取消选中所有这些吗?
  • 是的,我想取消选中所有
  • 你可以这样做: $('#button').click(function() { $('input[type=checkbox]').prop('checked', true); }); -- 选择器将扫描所有输入控件并通过'checkbox'类型对其进行过滤,然后内部代码会将其'checked'属性切换为'true'。

标签: javascript jquery asp.net checkboxlist


【解决方案1】:

可能Similar QUestion

    //Assuming you have this object model structure in your ASPX page.
<input type="text" name="openid_username" />
<input type="text" name="openid_identifier" />

Upon screen render, it gets  translated to:
<asp:TextBox ID="ctl00_ContentPlaceHolder1_ctl00_openid_username" runat="server"></asp:TextBox>
<asp:TextBox ID="ctl00_ContentPlaceHolder1_ctl00_openid_identifier" runat="server"></asp:TextBox>

<input type='button' id='myButton' value='Check Button'>

您可以通过下面的 jquery 代码设置复选框的选中属性:

$('#myButton').click(function() {
  $('input[name$=openid_username]').prop('checked',true);
$('input[name$=openid_identifier]').prop('checked',true);
  });

另外,请参阅这个 jsFiddle link

【讨论】:

    【解决方案2】:

    给复选框一个 css 类说 .mycheckbox

    // On:
    $(".mycheckbox").checked(true);
    
    // Off:
    $(".mycheckbox").checked(false);
    
    // Toggle:
    $(".mycheckbox:checked").checked(false);
    $(".mycheckbox:not(:checked)").checked(true);
    

    【讨论】:

    • 嗨汤姆,复选框是从数据库动态绑定的。这个解决方案在这种情况下会起作用吗?感谢您的回复
    • user545359,是的,它应该不会破坏 CssClass 名称。 Asp.net 会破坏 ID,所以如果你坚持这样做,你应该没问题。
    【解决方案3】:

    jQuery

    ​​>
    function checkAll(formname)
    {
      var checkboxes = new Array(); 
      checkboxes = document[formname].getElementsByTagName('input');
    
      for (var i=0; i<checkboxes.length; i++)  {
        if (checkboxes[i].type == 'checkbox')   {
          checkboxes[i].checked = false;
        }
      }
    }; 
    
    $(document).ready(function(){
    $('#unCheck').click(function () {
             checkAll("form1");
          });
    });
    

    HTML

      <form id="form1" runat="server">
        <div>
        <asp:CheckBoxList id="check1" AutoPostBack="True" TextAlign="Right" runat="server">
        <asp:ListItem>Item 1</asp:ListItem>
        <asp:ListItem>Item 2</asp:ListItem>
        <asp:ListItem>Item 3</asp:ListItem>
        <asp:ListItem>Item 4</asp:ListItem>
        <asp:ListItem>Item 5</asp:ListItem>
        <asp:ListItem>Item 6</asp:ListItem>
    


    这将解决您的问题。

    【讨论】:

      【解决方案4】:

      放一个html按钮:

      <input type="button" value="Uncheck all" onclick="uncheckCheckboxes()" />
      

      编写javascript函数uncheckCheckboxes(假设你的checkboxlist id是cbl):

      function uncheckCheckboxes()
      {
         $("#<%# cbl.ClientID %> input").prop("checked",false);
      }
      

      【讨论】:

      • 下面不工作应该工作 $("#").prop("checked",false);
      猜你喜欢
      • 2013-05-26
      • 1970-01-01
      • 2014-02-11
      • 1970-01-01
      • 2018-08-01
      • 1970-01-01
      • 2015-02-23
      • 1970-01-01
      • 2014-06-24
      相关资源
      最近更新 更多