无法启用默认情况下禁用的 XHTML 提交输入/按钮
我在 XHTML 中有一个简单的按钮:
<input type="submit" name="submit" value="Test" disabled="disabled" onmouseover="this.disabled=''" />
问题是,无论我尝试什么,当我将鼠标悬停在该按钮上时,它都不会从其禁用的属性中重新启用。在 XHTML 中,您需要使用 disabled="disabled"
这似乎完全破坏了使用 JavaScript 启用/禁用它的选项。我尝试过运行 this.disabled=''
、this.disabled=false
甚至 this.removeAttribute('disabled')
但是似乎没有什么能够重新启用该按钮。奇怪的是,如果我删除属性的 ='disabled'
部分(使其无效 XHTML),脚本会正常启用该按钮。如果不使用无效的 XHTML,这是否不可能?
注意: 我真的更愿意在这个特定示例中仅使用 JavaScript,而不是 jQuery。
我以为这会是一件简单的事情,大约需要 5 秒钟,但显然不是。
I have this simple button in XHTML:
<input type="submit" name="submit" value="Test" disabled="disabled" onmouseover="this.disabled=''" />
The problem is, no matter what I try, when I hover over the button, it won't re-enable from the disabled attribute it has. In XHTML, you are required to use disabled="disabled"
which seems to completely break the option to enable/disable it with JavaScript. I've tried running this.disabled=''
, this.disabled=false
, and even this.removeAttribute('disabled')
but nothing seems to be capable of re-enabling the button. Weird thing is, if I remove the ='disabled'
part of the attribute (making it invalid XHTML), the script enables the button just fine. Is this not possible without using invalid XHTML?
Note: I'd really prefer to only use JavaScript for this specific example, not jQuery.
I thought this would be something simple that would take like 5 seconds but apparently not.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于某种原因,禁用的元素似乎不会随着单击而触发鼠标悬停/移出事件。
以下不是世界上最好的解决方案,但您可以将其包装在另一个元素中,并使用包装元素的 mouseover 事件来启用它。
Disabled elements for some reason do not seem to fire mouseover/out events along with click.
The following is not the best solution in the world, but you can wrap it in another element and use the wrapping element's mouseover event to enable it.
disabled=false
是正确的。问题是禁用的元素不会接收事件。请参阅问题Javascript OnMouseOver 和 Out 禁用/重新启用项目问题。
disabled=false
is correct.The problem is that a disabled element doesn't receive events. See the question Javascript OnMouseOver and Out disable/re-enable item problem.