2009-08-18 72 views
4

我通过javascript设置只读=“只读”(换句话说,TRUE):文本输入只读属性在IE7中无法识别?

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

这是有预期的效果(使现场无法再修改,但其内容与形式提交)在FF,Safari和Chrome中,但不适用于IE7。在IE7中,我仍然可以修改文本输入字段的内容。

我试过设置(“只读”,“真”),它在我测试的所有其他三种浏览器中都能正常工作,但IE7也忽略了这一点。

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

回答

13

你试过这个吗?

document.getElementById("my_id").readOnly = true; 
2

尝试:

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

readOnly的Ø就是资本!

+0

该属性在HTML中不区分大小写,在XHTML中全部小写。这看起来像通常的Internet Explorer setAttribute错误。安全的解决方案是避免设置属性,并使用accessor属性(请参阅vit的答案)。 – Quentin 2009-08-18 16:01:51

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> 
0

或者试试:

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

请注意由@TheVillageIdiot指出,以只读的O为大写。这应该从IE7到IE11。