使用 JavaScript 使文本框可编辑

2022-01-13 00:00:00 textbox javascript readonly

我有一个带有 readonly="readonly" 的文本框,这意味着我无法对其进行编辑.但我想要的是让这个文本框在用户双击它时可编辑.

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.

我尝试过的是:

<input size="10" readonly="readonly" ondblclick="setEditable(this)"/>

在 JavaScript 中:

and in 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?

推荐答案

更新:

再次使其只读:

var el = document.getElementById('txt');
el.onblur = function(){
  this.setAttribute('readonly');
};

<小时>

你可以这样做:


You can do this:

<input size="10" readonly="readonly" id="txt" />

JS:

var el = document.getElementById('txt');
el.ondblclick = function(){
  this.removeAttribute('readonly');
};

相关文章