2014-02-06 52 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"