0

我创建了这个 jQuery 来启用/禁用 asp.net 面板中的子表单元素,这个脚本将启用编辑表单,但在我第二次点击后它不会禁用它有人可以帮我吗?

 <script type="text/javascript">
        $(function () {
            //creating toggle
            $("#check").button();

            $("[id$='check']").data('isenabled', true); //enabled assumption
            //disabled all input form 
            $("[id$=p_taskInfo]").children().prop("disabled", "disabled");
            $("input[name='check']").click(function () {
                var currentState = $(this).data('isenabled');
                if (currentState) {
                    $("[id$=p_taskInfo]").children().removeProp("disabled");
                }
                else {
                    $("[id$=p_taskInfo]").children().prop("disabled", "disabled");
                }
                $(this).data('isenabled', !currentState);
            }); //EOF click function 
        });//EOF function 
    </script>
4

1 回答 1

0

currentState拼写错误,您需要交换 if...else 块中的两个语句,如下所示:

 <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
    <script src="Scripts/jquery-1.8.3.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("[id$=p_taskInfo]").children().prop("disabled", "disabled");
            $("input[name='check']").click(function () {
                var currentState = $(this).data('isenabled');
                if (currentState) {
                    $("[id$=p_taskInfo]").children().prop("disabled", "disabled");
                }
                else {
                    $("[id$=p_taskInfo]").children().removeProp("disabled");
                }
                $(this).data('isenabled', !currentState);
            });
        });
    </script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <input id="check" name="check" type="button" value="Enable\Disable" />
    <asp:Panel ID="p_taskInfo" runat="server">
        <asp:Button ID="Button2" runat="server" Text="Button" />
        <asp:Button ID="Button3" runat="server" Text="Button" />
    </asp:Panel>
</asp:Content>
于 2013-01-02T19:59:06.007 回答