javascript 使用 jquery 禁用链接按钮(锚标记)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15903660/
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-10-27 02:31:30  来源:igfitidea点击:

Disable linkbutton (anchor tag) with jquery

jquerylinkbuttonjavascript

提问by J.Olsson

I have trouble with my jquery disabling my linkbutton from clicks after the first click has taken place.

在第一次点击发生后,我的 jquery 无法点击我的链接按钮时遇到问题。

The click should also remove and add some cssStyles.

单击还应删除并添加一些 cssStyles。

The problem is no that the linkbutton (anchortag rendered in html) dosent get disabled after the cssStyles has been changed.

问题不在于在更改 cssStyles 后链接按钮(以 html 呈现的锚标记)不会被禁用。

After a postback(triggered manually by me) it goes disabled. but that is not something i want. i want it to be a smooth transaction and no postback.

回发(由我手动触发)后,它会被禁用。但这不是我想要的。我希望它是一个顺利的交易,没有回发。

MarkupCode:

标记代码:

<asp:LinkButton ID="LinkButton" runat="server" OnClientClick="" CommandName="Answer" CssClass="linkButton" >
   <asp:Panel ID="PoptionText" runat="server" CssClass="buttonStyle">
      <%# Eval("optionText") %>
      <div>
         <asp:Label ID="lbR?tt" runat="server" Visible="false" CssClass="aktiv"></asp:Label>
      </div>
   </asp:Panel>
</asp:LinkButton>

Javascript code:

Javascript代码:

$(document).ready(function () {
    $("#LinkButton").click(function () {    
        $("#LinkButton").attr("disabled", "disabled"); //this does not happen
        Rightanswer(); // This happens. Function to add remove cssStyles
    })
    $("#LinkButton").click(function (e) {
        if ($("#LinkButton").attr("disabled") == "disabled") {
            e.preventDefault();
        }
    });    
});

回答by Dhaval Marthak

You can also try this one out:

你也可以试试这个:

$("#<%=LinkButton.ClientID %>").click(function (e) {
   e.preventDefault();
   $(this).attr("disabled", "disabled");
   // Whateverr your code goes here
});

回答by xyz

You cant use disabledattribute with anchor tag, only inputelements have disabledattribute. You can try something like

您不能将disabled属性与锚标记一起使用,只有input元素具有disabled属性。你可以尝试类似的东西

$("#LinkButton").click(function (e) {
     if ($("#LinkButton").hasClass(className)) {
        // If have class dont follow href so will work only for first time 
        e.preventDefault();
     }
     Rightanswer(); // Add / Remove class
});      

This might help Should the HTML Anchor Tag Honor the Disabled Attribute?

这可能有助于HTML Anchor 标签是否应该尊重 Disabled 属性?