5

我正在通过 javascript 设置 readonly="readonly" (换句话说,true):

document.getElementById("my_id").setAttribute("readonly", "readonly");

这在 FF、Safari 和 Chrome 中具有预期的效果(使该字段不再可编辑,但其内容与表单一起提交),但不适用于 IE7。在 IE7 中我仍然可以修改文本输入字段的内容。

我也尝试过设置 ("readonly", "true"),它适用于我正在测试的所有其他三种浏览器,但 IE7 也忽略了它。

有没有人有尝试用 IE7 做到这一点的经验?我不想使用 disabled 属性,因为我希望文本输入字段中的值与表单一起提交。

4

4 回答 4

13

你试过这个吗?

document.getElementById("my_id").readOnly = true;
于 2009-08-18T15:43:52.577 回答
2

尝试:

document.getElementById("my_Id").setAttribute("readOnly","readonly")

它是readOnlyO是大写!

于 2009-08-18T15:57:13.737 回答
2
<script type="text/javascript">

function blah(txt) {
   var text = document.getElementById(txt).value;
   if(text.length > 4 && document.getElementById('chk').checked == false) {

   // *********    NOT WORKING WITH IE   *************** //
   //document.getElementById("ta").setAttribute('readOnly','readonly');
   //document.getElementById("ta").readOnly="readOnly";
   // ******** ---   **********//

   //document.getElementById("ta").disabled = true;   //  for disable textArea
   document.getElementById("ta").blur(); // comment this when above line is uncommented or visa-versa
   document.getElementById('chkBox').style.display = "block";
   }
}

function unable() {
  // document.getElementById("ta").disabled = false; // to unable textArea -- uncomment when disabling is used or visa-versa
   document.getElementById('ta').focus();
   document.getElementById('chkBox').style.display = "none";
}

</script>


<textarea id="ta" onkeydown="blah('ta')" ></textarea>
<div id="chkBox" style="display:none"><label> Please check this to continue....</label><input type="checkbox" id="chk" onclick="unable()"></div>
于 2010-09-13T09:09:19.423 回答
0

或尝试:

document.getElementById("my_id").setAttribute("readOnly", true);

请注意@TheVillageIdiot 所述, readOnly 中的 O 是 UPPER CASE。这应该适用于 IE7 到 IE11。

于 2016-01-05T15:06:30.423 回答