Jquery 启用和禁用 Asp.net 按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5613064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 19:29:10  来源:igfitidea点击:

Jquery to enable and disable a Asp.net button

jqueryasp.net

提问by kalls

I have a <asp:Button ID="BtnAddCart" runat="server" Text="Place Order" Width="147px" onclick="BtnAddCart_Click" Enabled="false"/>

我有一个 <asp:Button ID="BtnAddCart" runat="server" Text="Place Order" Width="147px" onclick="BtnAddCart_Click" Enabled="false"/>

and a checkbox column and checkbox header on a gridview. on the header you select the checkbox all items on the grid are selected. If you uncheck the header checkbox the items on the grid are un selected.

以及网格视图上的复选框列和复选框标题。在标题上选择复选框,网格上的所有项目都被选中。如果您取消选中标题复选框,则不会选择网格上的项目。

here is the Jquery

这是 Jquery

  <script type="text/javascript">
    $(document).ready(function () {
        var chkBox = $("input[id$='ChkAll']");
        chkBox.click(
           function () {
               $("#MainContent_ProductGrid INPUT[type='checkbox']")
             .attr('checked', chkBox
              .is(':checked'));
               $("#MainContent_BtnAddCart INPUT[type='submit']").removeAttr('disabled');
           });
        // Uncheck      
        $("#MainContent_ProductGrid INPUT[type='checkbox']").click(
                    function (e) {
                        if (!$(this)[0].checked) {
                            chkBox.attr("checked", false);
                        }
                        $("#MainContent_BtnAddCart INPUT[type='submit']").attr('disabled', 'disabled');

                    });
    });
</script>

why is the button not getting enabled and disabled.

为什么按钮没有被启用和禁用。

采纳答案by tvanfosson

You are missing a parenthesis in your click handler for the checkbox. Is that a typo? I'd probably rewrite it to accommodate setting button visibility both for check all and when any checkbox is clicked, sharing the same code. Note that if you use @SLaks suggestion you may be able to simplify the selectors for the grid and button. I've used the "endsWith" selector in all cases to deal with ASP.NET name mangling.

您在单击处理程序中缺少复选框的括号。这是一个错字吗?我可能会重写它以适应设置按钮可见性,以便在全部选中和单击任何复选框时共享相同的代码。请注意,如果您使用 @SLaks 建议,您可能能够简化网格和按钮的选择器。我在所有情况下都使用“endsWith”选择器来处理 ASP.NET 名称修饰。

<script type="text/javascript">
    $(document).ready(function () {
        var $chkAll = $('input[id$="ChkAll"]');
        var $boxes = $('[id$="ProductGrid"] input:checkbox').not($chkAll);

        // update all checkboxes and submit button when check all is toggled
        $chkAll.click( function() {
           if ($chkAll.is(':checked')) {
              $boxes.attr('checked','checked');
           }
           else {
              $boxes.attr('checked','');
           }
           setButtonState();
        });

        // when any checkbox is clicked, update submit button state
        $boxes.click( function() {
           setButtonState();
        });

        // if any checkbox is checked, button is enabled, else disabled.
        function setButtonState()
        {
            var state = 'disabled';
            $boxes.each( function() {
               if ($(this).is(':checked')) {
                  state = '';
                  return false;
               }
            });
            $('[id$="BtnAddCart"]').attr('disabled',state);
        }

    });
</script>

回答by Paul Gorbas

Just for sake of completeness - here is how to toggle asp button on and off. In This example I default the button to disabled:

只是为了完整起见 - 这里是如何打开和关闭 asp 按钮。在这个例子中,我默认禁用按钮:

In .aspx

在.aspx

<asp:Button ID="btnNext" runat="server" Text="Next" CssClass="cssBtnNext_Disabled" Enabled="false" />

In jQuery enabled javaScript:

在启用 jQuery 的 javaScript 中:

<script src="/js/jquery/jquery-1.4.2.min.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">

    $().ready(function()
    {
       var btnNext = $("#<%= btnNext.ClientID %>"); // get the ASP mangled id//$("#btnNext");
       btnNext.click(NextClicked);

       function NextClicked()
       {
          alert("NextClicked Fired!");
       }

       ToggleButtonEnable( IsEnabled )//IsEnabled is true or false
       { 
           if (IsEnabled )
           {//was disabled, now enable
          btnNext.removeAttr("disabled"); //enable button
          btnNext.attr("class", "cssBtnNext_Enabled");
           }
           else
           {//was enabled, now disable
              btnNext.attr("disabled", "disabled"); //disable button
              btnNext.attr("class", "cssBtnNext_Disabled");
           }
       }


}

</script>

回答by Ng? ??c Tu?n

I can fix your problems: $(".classButton").prop('disabled','disabled');

我可以解决您的问题: $(".classButton").prop('disabled','disabled');

回答by SLaks

By default, ASP.Net generates its own IDs.

默认情况下,ASP.Net 生成自己的 ID。

Set ClientIdMode="Static"for the button to force it to use your ID.
Alternatively, write <%= BtnAddCart.ClientID %>to get the generated ID.

设置ClientIdMode="Static"按钮以强制它使用您的 ID。
或者,写入<%= BtnAddCart.ClientID %>以获取生成的 ID。