使用jquery切换LinkBut​​ton的启用/禁用状态 [英] Toggling enabled/disabled state of a LinkButton using jquery

查看:98
本文介绍了使用jquery切换LinkBut​​ton的启用/禁用状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要使用jquery启用或禁用的LinkBut​​ton(最初禁用),但似乎使用我正在使用的方法禁用按钮会删除href属性,所以当我重新启用它时,按钮看起来已启用,但它无法点击。

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');

启用/禁用LinkBut​​tons的正确方法是什么?

What's the correct way to enable/disable LinkButtons?

推荐答案

问题是客户端浏览器中控件的ID是 lbSave 但WebForms会把它变成类似 ctl00_nameofanynestedaspnetcontainercontrols_morenestedcontainernames_lbSave。

The 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.

Praveen建议的解决方法是,不要尝试通过id找到控件,但是给它一个唯一的类名 - webforms不会破坏 - 并且问题解决了。

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.

另一种方法是将asp.net发送到在你的标记中插入正确的ID:

The alternative is to get asp.net to plug the correct id in for you in your markup:

启用:
$('#<%= lbSave.ClientID %> ')。attr('禁用','');

Enable: $('#<%= lbSave.ClientID %>').attr('disabled', '');

禁用:
$('# <%= lbSave.ClientID %> ')。removeAttr('disabled');

Disable: $('#<%= lbSave.ClientID %>').removeAttr('disabled');

(如果你是使用Asp.Net 4 Web形成此页 http://weblogs.asp.net/scottgu/archive/2010/03/30/cleaner-html-markup-with- asp-net-4-web-forms-client-ids-vs-2010-and-net-4-0-series.aspx 为您提供了另一种更简单的解决方案。)

(If you're using Asp.Net 4 WebForms this page http://weblogs.asp.net/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).

但如果你在Asp.Net 4上做新项目,你真的很想使用Asp.Net MVC,所有这些问题都会消失。

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.

这篇关于使用jquery切换LinkBut​​ton的启用/禁用状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆