启用checkboxlist不能在IE,asp.net,jquery中工作

时间:2021-03-07 03:18:09
<asp:CheckBoxList ID="chkList" runat="server" Enabled="false" >
    <asp:ListItem Text="1" Value="1"></asp:ListItem>
    <asp:ListItem Text="2" Value="2"></asp:ListItem>
</asp:CheckBoxList>

<asp:Button ID="btnFoo" runat="server" Text="Test" />

<script type="text/javascript" language="javascript">
    $(document).ready(function () {
        $('#<% = btnFoo.ClientID %>').click(function () {
            //var targetValue = 2;
            var items = $('#<% = chkList.ClientID %> input:checkbox');
            for (var i = 0; i < items.length; i++) {
                //alert(i);
                //if (items[i].value == targetValue) {
                    items[i].checked = true;
                    // break;
                //}
            }

            $('#<%= chkList.ClientID%>input:checkbox').removeAttr('disabled');                                                    return false;
        })
    });
</script>

Note: it works in Chrome, FF not in IE

注意:它适用于Chrome,FF不适用于IE

Its not wroking in IE8, here is the below code. Its checking all checkboxes, but keeps them Disabled, any solution please?

它不在IE8中,这是下面的代码。它检查所有复选框,但保持禁用,任何解决方案好吗?

4 个解决方案

#1


1  

Try using the prop property instead of .checked:

尝试使用prop属性而不是.checked:

$("#btn").on("click",function(){
    $('input:checkbox').each(function(){
     $(this).prop("checked", "true");
    });
});

jsFiddle: http://jsfiddle.net/hxS7M/1/

jsFiddle:http://jsfiddle.net/hxS7M/1/

Edit: This will also make them enabled.

编辑:这也将使他们启用。

$("#btn").on("click",function(){
    $('input:checkbox').each(function(){
     $(this).prop("checked", "true").prop("disabled",false);
    });
});

jsFiddle: http://jsfiddle.net/hxS7M/4/

jsFiddle:http://jsfiddle.net/hxS7M/4/

#2


1  

Everybody's script is working for normal HTML page. However, you are using button control and it posts back to server unless you explicitly cancel the event.

每个人的脚本都适用于普通的HTML页面。但是,您正在使用按钮控件,并且它会回发到服务器,除非您明确取消该事件。

checkAll returns false to onclick event, so that it won't post back to server.

checkAll向onclick事件返回false,因此它不会回发给服务器。

启用checkboxlist不能在IE,asp.net,jquery中工作

<%@ Page Language="C#" AutoEventWireup="true" 
CodeBehind="WebForm7.aspx.cs" Inherits="WebApplication2010.WebForm7" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:CheckBoxList ID="chkList" runat="server" Enabled="False">
            <asp:ListItem Text="1" Value="1"></asp:ListItem>
            <asp:ListItem Text="2" Value="2"></asp:ListItem>
        </asp:CheckBoxList>
        <asp:Button ID="btnFoo" runat="server" Text="Test" 
            OnClientClick="return checkAll();" />
        <script type="text/javascript" language="javascript">
            function checkAll() {
                $('#<% = chkList.ClientID %> :checkbox').each(function () {
                    $(this).prop("checked", "true").prop("disabled",false);
                });
                return false;
            }
        </script>
    </div>
    </form>
</body>
</html>

Credit to @Hanlet Escaño.

感谢@HanletEscaño。

#3


0  

Try this:

尝试这个:

$(document).ready(function () {
  $('#btnFoo').click(function () {
     $('#chkList input:checkbox').each(function(i,e) {
        $(e).attr("checked", true);                       
     })
   });
})

#4


0  

To enable them, do this

要启用它们,请执行此操作

$('#<%= chkList.ClientID%>input:checkbox').prop('disabled', false);

#1


1  

Try using the prop property instead of .checked:

尝试使用prop属性而不是.checked:

$("#btn").on("click",function(){
    $('input:checkbox').each(function(){
     $(this).prop("checked", "true");
    });
});

jsFiddle: http://jsfiddle.net/hxS7M/1/

jsFiddle:http://jsfiddle.net/hxS7M/1/

Edit: This will also make them enabled.

编辑:这也将使他们启用。

$("#btn").on("click",function(){
    $('input:checkbox').each(function(){
     $(this).prop("checked", "true").prop("disabled",false);
    });
});

jsFiddle: http://jsfiddle.net/hxS7M/4/

jsFiddle:http://jsfiddle.net/hxS7M/4/

#2


1  

Everybody's script is working for normal HTML page. However, you are using button control and it posts back to server unless you explicitly cancel the event.

每个人的脚本都适用于普通的HTML页面。但是,您正在使用按钮控件,并且它会回发到服务器,除非您明确取消该事件。

checkAll returns false to onclick event, so that it won't post back to server.

checkAll向onclick事件返回false,因此它不会回发给服务器。

启用checkboxlist不能在IE,asp.net,jquery中工作

<%@ Page Language="C#" AutoEventWireup="true" 
CodeBehind="WebForm7.aspx.cs" Inherits="WebApplication2010.WebForm7" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:CheckBoxList ID="chkList" runat="server" Enabled="False">
            <asp:ListItem Text="1" Value="1"></asp:ListItem>
            <asp:ListItem Text="2" Value="2"></asp:ListItem>
        </asp:CheckBoxList>
        <asp:Button ID="btnFoo" runat="server" Text="Test" 
            OnClientClick="return checkAll();" />
        <script type="text/javascript" language="javascript">
            function checkAll() {
                $('#<% = chkList.ClientID %> :checkbox').each(function () {
                    $(this).prop("checked", "true").prop("disabled",false);
                });
                return false;
            }
        </script>
    </div>
    </form>
</body>
</html>

Credit to @Hanlet Escaño.

感谢@HanletEscaño。

#3


0  

Try this:

尝试这个:

$(document).ready(function () {
  $('#btnFoo').click(function () {
     $('#chkList input:checkbox').each(function(i,e) {
        $(e).attr("checked", true);                       
     })
   });
})

#4


0  

To enable them, do this

要启用它们,请执行此操作

$('#<%= chkList.ClientID%>input:checkbox').prop('disabled', false);