试图在checkboxlist中启用复选框?

时间:2011-12-14 09:55:47

标签: javascript jquery asp.net .net drop-down-menu

我正在尝试启用复选框列表中的复选框。在我的解决方案中唯一不起作用的是从IE9中的span中删除disabled属性,它在FF中工作:

HTML

<!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="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    <script type="text/javascript">

        function enablebut() {
            $('#CheckBoxList1_0').removeAttr('disabled');
            $('#CheckBoxList1_0').closest('span').removeAttr('disabled');

        }

    </script>
</head>
<body>
    <form id="form1" runat="server">
    <button onclick="enablebut();">enable</button>
    <div>
        <asp:CheckBoxList ID="CheckBoxList1" Enabled='false' runat="server">
            <asp:ListItem Text="een"></asp:ListItem>
            <asp:ListItem Text="twee"></asp:ListItem>
        </asp:CheckBoxList>
    </div>
    </form>
</body>
</html>

3 个答案:

答案 0 :(得分:1)

添加type="button"属性以阻止提交(回发)。

 <button type="button" onclick="enablebut();">enable</button>

或者

<input type="button"  onclick="enablebut();" value="enable"/>

答案 1 :(得分:0)

我认为不是通过javascript传递函数,而是应该在document.ready函数中编写以下代码,因为你使用的是jQuery

$(document).ready(function () {
            $('#CheckBoxList1_0').attr('disabled','');
    })

编辑:整页代码:

 <!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="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    <script type="text/javascript">

       $(document).ready(function () {
            $('#CheckBoxList1_0').attr('disabled','');
    })
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <button type="button">enable</button>
    <div>
        <asp:CheckBoxList ID="CheckBoxList1" Enabled='false' runat="server">
            <asp:ListItem Text="een"></asp:ListItem>
            <asp:ListItem Text="twee"></asp:ListItem>
        </asp:CheckBoxList>
    </div>
    </form>
</body>
</html>

答案 2 :(得分:0)

它在IE8中没有变化,这是下面的代码

<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;
                            //}
                        }

$('#&lt;%= chkList.ClientID%&gt; input:checkbox')。removeAttr('disabled');返回false;                         })                     });