使用 JavaScript 使文本框可编辑
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5016326/
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
Make text box editable using JavaScript
提问by Harry Joy
I've a textbox with readonly="readonly"
that means I can not edit it. But what I want is to make this textbox editable when user double clicks on it.
我有一个文本框,readonly="readonly"
这意味着我无法编辑它。但我想要的是当用户双击它时使这个文本框可编辑。
What I've tried yet is:
我已经尝试过的是:
<input size="10" readonly="readonly" ondblclick="setEditable(this)"/>
and in JavaScript:
在 JavaScript 中:
function setEditable(i){
i.readonly = false;
}
But this does not worked. So how can I make a textbox editable, which is readonly, when user double clicks on it?
但这不起作用。那么当用户双击它时,如何使文本框可编辑,这是只读的?
回答by Sarfraz
Update:
更新:
To make it readonly again:
再次使其成为只读:
var el = document.getElementById('txt');
el.onblur = function(){
this.setAttribute('readonly');
};
You can do this:
你可以这样做:
<input size="10" readonly="readonly" id="txt" />
JS:
JS:
var el = document.getElementById('txt');
el.ondblclick = function(){
this.removeAttribute('readonly');
};
回答by Shaun Hare
as above pure javascript example by sarfraz try to make your javascript unobtrusive much better practice
如上 sarfraz 的纯 javascript 示例,尝试使您的 javascript 不显眼,更好地实践
Also as a lot of people do if you have jquery on page now you can use that to do same
如果您现在在页面上有 jquery,那么很多人也会这样做,您可以使用它来做同样的事情
In jquery
在jQuery中
$('#txt').removeAttr("readonly");
$('#txt').removeAttr("只读");
回答by Shrikant Patil
To make text field editable
使文本字段可编辑
document.getElementById("TextFieldId").readOnly=true;
To make text field Uneditable
使文本字段不可编辑
document.getElementById("TextFieldId").readOnly=false;