簡體   English   中英

在單擊按鈕時禁用對文本框的只讀

[英]Disable readonly to text box onclicking the button

我對文本框使用了“只讀”屬性,這使得文本框不可編輯,我是否知道如何在單擊按鈕時禁用只讀屬性。 我們可以使用任何腳本來做到這一點嗎?

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

你可以做兩件事:

HTML:

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

JS(選項 1):[演示]

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

JS(選項 2):[演示]

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

嗯,簡單點! 您可以使用 jQuery 刪除此屬性(jQuery 很簡單,您也可以使用 JS 來完成此操作)。 這是代碼:

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

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

您需要將元素的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;
};

演示

此外,我建議您使用'type="button"'而不是type="submit"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM