0
<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>

注: Chrome では動作しますが、FF は IE では動作しません。

IE8では機能しません。以下のコードです。すべてのチェックボックスをチェックしていますが、それらを無効のままにしています。解決策はありますか?

4

4 に答える 4

1

みんなのスクリプトは通常の HTML ページで動作します。ただし、ボタン コントロールを使用しているため、明示的にイベントをキャンセルしない限り、サーバーにポスト バックされます。

checkAllサーバーにポストバックしないように、onclick イベントに false を返します。

ここに画像の説明を入力

<%@ 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>

@Hanlet Escaño の功績。

于 2013-04-05T20:14:26.167 に答える
1

propの代わりにプロパティを使用してみてください.checked:

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

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

編集:これにより、それらも有効になります。

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

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

于 2013-04-05T18:21:04.043 に答える
0

それらを有効にするには、これを行います

$('#<%= chkList.ClientID%>input:checkbox').prop('disabled', false);
于 2013-04-05T18:42:06.453 に答える