0

我有以下 HTML 表单代码:

<input type="text" name="myText" value="Enter Your Name" readonly>
<input type="button" onclick="inn()" value="edit">

当我单击编辑按钮时,我希望删除文本字段的只读属性,以便用户可以编辑文本字段的值。我不想更改任何其他属性。

我也不想编辑整个表单:

formId.innerHTML=

上面的代码只显示了一个文本字段和一个按钮。在我的页面上有 12 个文本字段和 5 个按钮。我想删除该属性,而不是再次重新编辑整个属性。

请提出一些有用的 JavaScript 代码。

function inn()
{
   // What do I type here to remove the readonly property of the "MyText" text-field?
}
4

3 回答 3

3

首先,您需要<input>从 DOM 中获取元素。一种可能的方式:

var element = document.getElementsByName("myText")[0];

然后,要删除,readonly您可以更改readOnly属性:

element.readOnly = false;

或显式删除该属性:

element.removeAttribute("readonly");
于 2013-03-10T11:44:15.410 回答
2

尝试:

为您的输入元素提供一个 ID:

HTML

<input type="text" name="myText" id="myText" value="Enter your name" readonly />

JS

function inn() {
    document.getElementById('myText').readonly = false;
}
于 2013-03-10T11:44:22.937 回答
0

首先为您的文本框分配一个 ID。

IE

<input type="text" name="myText" id ="txt" value="Enter Your Name" readonly>
<input type="button" onclick="inn()" value="edit">

然后你可以用你的javascript写 -

function inn() 
{ 
    if ($('#txt').attr("readonly") == true) 
    { 
        $('#txt').val(''); 
        $('#txt').removeAttr("readonly"); 
    } 
}
于 2013-03-10T11:52:47.167 回答