Javascript javascript启用双击输入

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

javascript enable input on double click

javascripthtmlforms

提问by Umbrella

I have a form with some inputs disabled. I want these inputs to be enabled when double clicked. Unfortunately, JS events seem not to fire on disabled inputs.

我有一个禁用了某些输入的表单。我希望在双击时启用这些输入。不幸的是,JS 事件似乎不会在禁用的输入上触发。

<input type="text" value="asdf" disabled="disabled" ondblclick="this.disabled=false;">?

Is there a way around this limitation?

有没有办法绕过这个限制?

Or am I just going to have to wrap any affected input in a span to house the event?

或者我是否只需要将任何受影响的输入包装在一个跨度中以容纳事件?

http://jsfiddle.net/vhktx/

http://jsfiddle.net/vhktx/

回答by JonWarnerNet

ondblclickdoes not get fired on a disabledelement, you should mark it as readonly:

ondblclick不会在disabled元素上触发,您应该将其标记为readonly

<input type="text" value="asdf" readonly="true" ondblclick="this.readOnly='';">

http://jsfiddle.net/vhktx/4/

http://jsfiddle.net/vhktx/4/

回答by Gabe

You will have to wrap the element in a container since the the event will not fire on disabled elements.

您必须将元素包装在容器中,因为该事件不会在禁用的元素上触发。

jsFiddle

js小提琴

<div ondblclick="document.getElementById('test').disabled=false;">
    <input type="text" id="test" value="asdf" disabled="disabled"></div>