2014-02-06 40 views
5

我已经使用“只读”属性的文本框,使文本框不可编辑,我可以知道如何禁用只读属性点击按钮。我们可以使用任何脚本来做到这一点吗?禁用只读文本框onclicking按钮

<input type="text" name="" value="" readonly="readonly"/><br/> 
<input type="submit" name="" value="update" onclick="" /> 
+0

的jQuery或JavaScript '类型= “按钮”',你有什么用? – Satpal

回答

7

你可以做两件事情:

HTML:

<input id="myInput" type="text" readonly="readonly" /><br /> 
<input id="myButton" type="submit" value="update" /> 

JS(选项1):[Demo]

document.getElementById('myButton').onclick = function() { 
    document.getElementById('myInput').removeAttribute('readonly'); 
}; 

JS(选项2):[Demo]

document.getElementById('myButton').onclick = function() { 
    document.getElementById('myInput').readOnly = false; 
}; 
+0

非常感谢! – Anand

2

好吧,要简单!你可以使用jQuery去除这个属性(jQuery会很简单,你也可以使用JS来做到这一点)。下面是该代码:

$('input[type=submit]').click(function() { 
    $('input[type=text]').removeAttr('readonly'); 
}) 

https://api.jquery.com/removeAttr/

1

需要设置的readOnly属性元素到false

您可以使用document.getElementById,它通过其ID

HTML参考元素,在这里,我添加了ID来元素

<input type="text" id="myTextBox" name="" value="" readonly="readonly"/><br/> 
<input type="submit" id="myButton" name="" value="update" /> 

的JavaScript,在这里你可以看到readOnly属性设置为false。

document.getElementById('myButton').onclick = function() { 
    document.getElementById('myTextBox').readOnly =false; 
}; 

DEMO

此外,我建议你使用的不是type="submit"