禁用JavaScript =真VS ASP.NET启用=假
在我的ASP.NET web应用程序,我有可能会在code,后面被关闭,某些元素可能在JavaScript中禁用某些元素。
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.
对于code-背后的元素,我只需使用下面的VB.NET属性:
For the code-behind elements, I simply use the following VB.NET property:
Me.element1.Enabled = False
有关动态元素,我用下面的JavaScript属性:
For the dynamic elements, I use the following JavaScript property:
document.getElementById('" & Me.element2.ClientID & "').disabled = true;
我注意到的第一件事就是更改Enabled属性不只是添加禁用=禁用
来标记,它也从按钮(我的皮肤类名按钮)按钮aspNetDisabled
。为了弥补这一点,我包括以下额外的JavaScript行:
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:
document.getElementById('" & Me.element2.ClientID & "').className = 'button aspNetDisabled';
(显然,这意味着我需要保持className属性,如果/当我再次启用的元素,我需要确保我用按钮
/ 文本
/等适用 - 对于加分,如果你对此有任何建议,我会很高兴听到他们的声音)
(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)
但是,我也注意到,通过JavaScript产生的标记仅禁用=
,而由ASP.NET Enabled属性变化产生的标记是禁用=禁用
。
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的摆弄它更改为:
I tried fiddling with the JavaScript to change it to:
document.getElementById('" & Me.element2.ClientID & "').disabled = 'disabled';
不过,这似乎并没有发挥作用,使用属性仍是空的。有趣的是,禁用= TRUE;
和禁用=禁用;
似乎以同样的方式工作 - 我想到一他们的是更正确的(我坚持 = TRUE;
现在)。
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).
有没有实现用JavaScript相同的结果与关于启用和禁用元素的服务器端ASP.NET的推荐的最佳方式?
和...
是渲染之差禁用=禁用
和禁用=
可能引起我任何问题?
Is the difference between the rendering of disabled="disabled"
and disabled=""
likely to cause me any problems?
的禁用
属性并不重要的价值,它的隐含 http://www.w3.org/TR/ HTML401 /互动/ forms.html#H-17.12.1
The value of the disabled
property doesn't matter, it is implied 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
奇怪的情况是
elem.disabled = ""; // disabled
通常,
是 falsy 的(如==假
是真正
)。
我会建议你用一个框架来设置这些属性捕捉任何浏览器的特定行为。对于禁用
没有太多的争斗,但性质一样透明度
不工作在所有浏览器相同的。
I would recommend you use a framework to set these properties to catch any browser specific behavior. For disabled
there isn't much strife but properties like opacity
don't work the same in all browsers.