[英]Making a text box read only after some data is fetched into it
我旁邊有一個文本框和一個控制按鈕,單擊該按鈕將調用另一個功能,在該功能中選擇了一些數據並將其寫回到文本框。當前,在將數據提取到文本框后,用戶可以編輯我的文本框文本框。我想使文本框僅在用戶單擊按鈕並將數據提取到文本框后才可讀,因此現在數據存在於無法編輯的文本框中。如果用戶必須更改數據,用戶必須再次單擊按鈕以調用該函數,這將幫助我們在文本框中覆蓋數據。如何在javascript中完成此操作。
你可以這樣
HTML:
<input type="text" id="mytextbox" value="click the button below."/>
<input type="button" value="click" onclick="changeText();"/>
Javascript:
function changeText(){
var text_box = document.getElementById('mytextbox');
if(text_box.hasAttribute('readonly')){
text_box.value = "This text box is editable.";
text_box.removeAttribute('readonly');
}else{
text_box.value = "This text box is read only.";
text_box.setAttribute('readonly', 'readonly');
}
}
小提琴示例: http : //jsfiddle.net/b7jAy/
您可以使用jQuery .attr()
函數設置文本字段的只讀狀態。
$('#textfield').attr('readonly', true);
$('#textfield').attr('readonly', 'readonly');
為文本框分配一個id
屬性,例如<input id=foo>
,並使用
document.getElementById('foo').readOnly = true;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.