[英]After Adding “readonly” attribute on text box not able to remove it in one event
復制步驟
使用Internet Explorer
我們無法在文本框中輸入
4.再次單擊文本框。 現在我們將能夠在文本框中輸入文本
我們嘗試過
1.將屬性設置為readOnly,即$('#myinput')。attr('readOnly',false);
2.調用$('#myinput')。click();
以下是HTML代碼
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Make input read only</title>
<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.4.min.js"></script>
</head>
<body>
<input id="myinput" type="text" />
<input id="mycheck" type="checkbox" />
<script type="text/javascript">
/*oncheck box click*/
$('#mycheck').click(function () {
if ($(this).attr('checked')) {
$('#myinput').attr('readOnly', 'readOnly');
}
else {
$('#myinput').removeAttr('readOnly');
/* also tried
* $('#myinput').attr('readOnly', false);
* $('#myinput').attr('readOnly', '');
*/
}
});
/*on text box click*/
$('#myinput').click(function () {
$('#mycheck').removeAttr('checked');
$('#myinput').removeAttr('readOnly');
/* also tried
* $('#myinput').attr('readOnly', false);
* $('#myinput').attr('readOnly', '');
*/
});
</script>
</body>
</html>
您需要將readonly屬性設置為類似這樣的值
$('#myinput').attr('readOnly', '');
我遇到了同樣的問題,並在stackoverflow上報告了它,一秒鍾后發現了它。
編輯: 找到了。
如果您閱讀了我的文章,它也可以解釋正在發生的事情。 實際上,文本框實際上已被解鎖(與Tyde嘗試的不同)。 問題與焦點/選擇有關。
function preventBackspace(e) {
var evt = e || window.event;
if (evt) {
if (evt.srcElement.getAttribute('readonly')) {
var keyCode = evt.charCode || evt.keyCode;
if (keyCode === 8) {
if (evt.preventDefault) {
evt.preventDefault();
} else {
evt.returnValue = false;
}
}
}
}
}
我有一個場景,其中的文本框將在只讀和可編輯之間切換,因此我添加了條件if (evt.srcElement.getAttribute('readonly'))
有所幫助。
將此用於IE,也可以在chrome和mozilla中使用
$('#myinput').removeAttr("readonly");
$('#myinput').select();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.