如何在 javascript 中启用/禁用链接按钮?

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

How do Enable/Disable linkbutton in javascript?

javascriptasp.net

提问by ar.gorgin

I have a link button in the page like:

我在页面中有一个链接按钮,例如:

<asp:LinkButton ID="edit" runat="server" OnClick="edit_Click" Enabled="False">??????</asp:LinkButton>

I want Enable/Disable this in javascript.

我想在 javascript 中启用/禁用它。

I use this code but set visible

我使用此代码但设置为可见

var objedit = document.getElementById('<%= edit.ClientID.ToString() %>');
objedit.style.display = "none";

I use this code but not enable

我使用此代码但未启用

if (count == 1) {
    objedit.disabled = false;
} else {
    objedit.disabled = true;
}

I can click but link button is disabled.

我可以点击但链接按钮被禁用。

enter image description here

在此处输入图片说明

回答by Peter Wateber

try this

试试这个

var objedit = document.getElementById("edit"); //not editid (editid is a variable)
objedit.disabled = true; //if I'm not mistaken its true/false or disabled

回答by u283863

So, is this what you want?
http://jsfiddle.net/hzaR6/
http://jsfiddle.net/hzaR6/2/-- UPDATED, tested in Chrome and Firefox

那么,这就是你想要的吗?-- 更新,在 Chrome 和 Firefox 中测试
http://jsfiddle.net/hzaR6/
http://jsfiddle.net/hzaR6/2/

The UPDATED way

更新的方式

You can use a classname to define disabled element, for which you can have more control on their styles ... etc.

您可以使用class名称来定义禁用元素,您可以对其样式进行更多控制......等。

$("#link").toggleClass("disabled"); //This will simply toggle the class

and for the css

和 css

#link.disabled{
    z-index:-1;            /*Make it not clickable*/
    position:relative;
    opacity: .5;           /*Lighter*/
}?

You can do whatever you want here.

你可以在这里为所欲为。

The good old formelement way

好的旧form元素方式

$("#edit").attr("disabled", false);
 -or-
document.getElementBy("edit").disabled = false;

This will disable any form element. If you want to enable them, just change falseto true.

这将禁用任何表单元素。如果要启用它们,只需更改falsetrue.



var a = document.getElementBy("edit").disabled;

awill be trueif the element is disabled. Otherwise it will be false.

atrue如果元素被禁用,将会是。否则就会false

回答by Daniel Szabo

This linkshould give you everything you need. You can't really "disable" a linkbutton because its just a link with some javascript attached. Basically, you need to reassign the click handlerto something that returns void or false.

这个链接应该给你你需要的一切。你不能真正“禁用”链接按钮,因为它只是一个附加了一些 javascript 的链接。基本上,您需要将点击处理程序重新分配给返回 void 或 false 的内容。

You can refer to the ID of the link with the following script:

您可以使用以下脚本引用链接的 ID:

<script runat="server">
   var a = document.getElementById('<%= edit.ClientID.ToString() %>')
</script>

回答by Moulika Pinapathruni

document.getElementById("lnk").style.display = "none";

回答by Alex Kudryashev

To disable element:

要禁用元素:

document.getElementById('<%# edit.ClientID %>').disabled = 'disabled';
//.ToString() is not necessary; ClientID is a string.

To re-enable:

重新启用:

document.getElementById('<%# edit.ClientID %>').disabled = '';

Of course, it can be done after document (DOM) is loaded.

当然,也可以在文档(DOM)加载后进行。