admin管理员组文章数量:1292123
I have a LinkButton (initially disabled) that needs to be enabled or disabled using jquery, but it seems like disabling the button with the method I'm using removes the href attribute, so when I re-enable it, the button looks enabled, but it's not clickable.
<asp:LinkButton Id="lbSave" runat="server" Text="Save" Enabled="False" ClientIDMode="Static" />
Disable:
$('#lbSave').attr('disabled', 'disabled');
Enable:
$('#lbSave').removeAttr('disabled');
What's the correct way to enable/disable LinkButtons?
I have a LinkButton (initially disabled) that needs to be enabled or disabled using jquery, but it seems like disabling the button with the method I'm using removes the href attribute, so when I re-enable it, the button looks enabled, but it's not clickable.
<asp:LinkButton Id="lbSave" runat="server" Text="Save" Enabled="False" ClientIDMode="Static" />
Disable:
$('#lbSave').attr('disabled', 'disabled');
Enable:
$('#lbSave').removeAttr('disabled');
What's the correct way to enable/disable LinkButtons?
Share Improve this question edited Oct 31, 2012 at 17:27 Prabhu asked Oct 31, 2012 at 17:09 PrabhuPrabhu 13.4k34 gold badges133 silver badges215 bronze badges 1- FWIW disabling an anchor client-side is not the same as disabling it server side (the rendered html is different via server side disabling) you cannot reliably block the anchor action via setting the disabled attribute client side and I would remend using a different type of html element or removing the href entirely from the anchor. – Quintin Robinson Commented Oct 31, 2012 at 17:14
4 Answers
Reset to default 5The problem is that the id of the control in the client browser is not lbSave
but WebForms will mangle it into something like ctl00_nameofanynestedaspnetcontainercontrols_morenestedcontainernames_lbSave.
The workaround as suggested by Praveen is, don't try to find the control by id but instead give it a unique classname - which webforms won't mangle - and voila problem solved.
The alternative is to get asp to plug the correct id in for you in your markup:
Enable: $('#<%= lbSave.ClientID %>').attr('disabled', '');
Disable: $('#<%= lbSave.ClientID %>').removeAttr('disabled');
(If you're using Asp.Net 4 WebForms this page http://weblogs.asp/scottgu/archive/2010/03/30/cleaner-html-markup-with-asp-net-4-web-forms-client-ids-vs-2010-and-net-4-0-series.aspx gives you an alternative simpler solution).
But if you're doing new projects in Asp.Net 4, you really really want to use Asp.Net MVC and all this kind of problem goes away.
You can do something like below:
<asp:LinkButton Id="lbSave" runat="server" Text="Save" Enabled="False" CssClass="lbSave" />
Disable:
$(".lbSave").attr("disabled", "disabled");
Enable:
$(".lbSave").removeAttr("disabled");
Hope this Helps!!
try this link disable a hyperlink using jQuery
so you can try
$('#<%= lbSave.ClientID %>').bind('click', false);
to enable
$('#<%= lbSave.ClientID %>').unbind('click', false);
Setting Enabled=False on an ASP.NET LinkButton removes the href attribute, so jquery can never enable it back. So here's a solution that works for me:
In javascript:
$(document).ready(function() {
_href = $('#lbSave').attr('href');
$('#lbSave').attr('disabled', 'disabled');
$('#lbSave').removeAttr('href');
});
Disable:
$('#lbSave').attr('disabled', 'disabled');
$('#lbSave').removeAttr('href');
Enable:
$('#lbSave').removeAttr('disabled');
$('#lbSave').attr('href', _href);
本文标签: javascriptToggling enableddisabled state of a LinkButton using jqueryStack Overflow
版权声明:本文标题:javascript - Toggling enableddisabled state of a LinkButton using jquery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741546904a2384646.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论