JavaScript 禁用 = true 与 ASP.NET 启用 = False

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

JavaScript disabled = true vs ASP.NET Enabled = False

javascriptasp.netmarkup

提问by oliver-clare

In my ASP.NET web application, I have some elements that may be disabled in code-behind and some elements may be disabled in JavaScript.

在我的 ASP.NET Web 应用程序中,我有一些元素可能在代码隐藏中被禁用,而一些元素可能在 JavaScript 中被禁用。

For the code-behind elements, I simply use the following VB.NET property:

对于代码隐藏元素,我只需使用以下 VB.NET 属性:

Me.element1.Enabled = False

For the dynamic elements, I use the following JavaScript property:

对于动态元素,我使用以下 JavaScript 属性:

document.getElementById('" & Me.element2.ClientID & "').disabled = true;

The first thing I noticed is that changing the Enabled property doesn't just add disabled="disabled"to the markup, it also changes the class from button(my skinned class name for buttons) to button aspNetDisabled. To compensate for this, I include the following additional JavaScript line:

我注意到的第一件事是,更改 Enabled 属性不仅会添加disabled="disabled"到标记中,还会将类从button(按钮的蒙皮类名称)更改为button aspNetDisabled. 为了弥补这一点,我添加了以下额外的 JavaScript 行:

document.getElementById('" & Me.element2.ClientID & "').className = 'button aspNetDisabled';

(Clearly, this means I need to maintain the className attribute if/when I enable the element again, and I'll need to ensure I use button/textbox/etc. as applicable - For bonus points, if you have any recommendations about this, I'd be happy to hear them)

(显然,这意味着如果/当我再次启用该元素时,我需要维护 className 属性,并且我需要确保我使用button/ textbox/etc。如果适用 - 对于奖励积分,如果您对此有任何建议,我很高兴听到他们)

However, I also noticed that the markup produced via JavaScript is only disabled="", whereas the markup produced by the ASP.NET Enabled property change is disabled="disabled".

但是,我也注意到通过 JavaScript 生成的标记仅为disabled="",而由 ASP.NET Enabled 属性更改生成的标记为disabled="disabled".

I tried fiddling with the JavaScript to change it to:

我尝试摆弄 JavaScript 将其更改为:

document.getElementById('" & Me.element2.ClientID & "').disabled = 'disabled';

However, this didn't seem to make a difference, with the attribute still being empty. Interesting that disabled=true;and disabled='disabled';seem to work in the same way - I expect one of them is more correct (I'm sticking to =true;for now).

但是,这似乎没有什么区别,属性仍然为空。有趣的是disabled=true;disabled='disabled';似乎以同样的方式工作 - 我希望其中一个更正确(我现在坚持=true;)。

Is there a recommended best way of achieving the same results with JavaScript as with server-side ASP.NET with regards to enabling and disabling elements?

在启用和禁用元素方面,是否有推荐的最佳方式使用 JavaScript 实现与使用服务器端 ASP.NET 相同的结果?

and...

和...

Is the difference between the rendering of disabled="disabled"and disabled=""likely to cause me any problems?

的渲染disabled="disabled"disabled=""可能会给我带来任何问题的差异?

采纳答案by Halcyon

The value of the disabledproperty doesn't matter, it is impliedhttp://www.w3.org/TR/html401/interact/forms.html#h-17.12.1

disabled属性的值无所谓,暗示http://www.w3.org/TR/html401/interact/forms.html#h-17.12.1

There is some strangeness going on though.

不过,有一些奇怪的事情正在发生。

var my_elem = document.getElementById('my_elem');
elem.disabled = true;  // disabled
elem.disabled = false;  // enabled
elem.disabled = "false"; // disabled

The strange case is

奇怪的情况是

elem.disabled = ""; // disabled

Normally ""is falsy(as in "" == falseis true).

通常""(如"" == falseis true)。

I would recommend you use a framework to set these properties to catch any browser specific behavior. For disabledthere isn't much strife but properties like opacitydon't work the same in all browsers.

我建议您使用框架来设置这些属性以捕获任何浏览器特定的行为。因为disabled没有太多冲突,但类似的属性opacity在所有浏览器中的工作方式都不相同。

回答by Richard

The disabled attribute doesn't have to have a fixed value - it used to work without ANY value (e.g. <input type=text disabled>) but this caused some validation problems. If you're already using jquery in your project, you can use something like:

disabled 属性不必有固定值——它过去没有任何值(例如<input type=text disabled>)也能工作,但这会导致一些验证问题。如果您已经在项目中使用 jquery,则可以使用以下内容:

$('#elementname').removeAttr('disabled').removeClass('aspNetDisabled');  // Enable

and

$('#elementname').attr('disabled').addClass('aspNetDisabled');  // Disable

The difference between disabled="disabled"and disabled="true"does not matter.

disabled="disabled"和之间的区别disabled="true"并不重要。