在单击按钮时禁用只读文本框 [英] Disable readonly to text box onclicking the button

查看:107
本文介绍了在单击按钮时禁用只读文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在文本框中使用了readonly属性,这使得文本框不可编辑,我可能知道如何在单击按钮时禁用readonly属性。我们可以使用任何脚本吗?

I have used "readonly" attribute to the textbox which makes the textbox non editable and may i know how to disable the readonly attribute on clicking the button. can we do this by using any script ?

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


推荐答案

你可以做两件事:


  • 删除 readonly (不区分大小写)属性,使用 removeAttribute

  • 设置< a href =https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement#Properties =noreferrer> readOnly (区分大小写)属性为 false

  • Remove the readonly (case insensitive) attribute, using removeAttribute
  • Set the readOnly (case sensitive) property to false

HTML:

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

JS(选项1):[ 演示 ]

JS (option 1): [Demo]

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

JS(选项2):[ 演示 ]

JS (option 2): [Demo]

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

这篇关于在单击按钮时禁用只读文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆