2013-03-10 59 views
0

我有一个HTML表单下面的代码:在Java脚本texfield删除属性

<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? 
} 

回答

3

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

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

随后,为了除去readonly你可以改变readOnly属性:

element.readOnly = false; 

或明确移除属性:

element.removeAttribute("readonly"); 
+0

为+1的removeAttribute – exexzian 2013-03-10 11:52:31

+0

雅老兄其工作。非常感谢那个兄弟。你是一个非常好的程序员。非常感谢 – 2013-03-10 12:06:28

2

尝试:

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

HTML

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

JS

function inn() { 
    document.getElementById('myText').readonly = false; 
} 
+0

兄弟不工作。为我提供任何其他解决方案。因为你建议我在这里是我的代码 - http://www.datafilehost.com/download-b5bb3c42.html和它的不工作 – 2013-03-10 11:57:56

+0

你有没有尝试@VisioN给的选项? – Vivienne 2013-03-10 12:01:13

+0

雅兄弟,一个工作.. – 2013-03-10 12:19:06

0

首先分配一个ID,你的文本。

<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"); 
    } 
} 
+0

OP没有提到jQuery的问题。 – VisioN 2013-03-10 11:53:43

+0

这一个也不适合我的。检查这个兄弟。是我犯了什么错误 - http://www.datafilehost.com/download-34696cc5.html – 2013-03-10 12:03:44