2009-08-18 74 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。