2013-10-18 37 views
1

我有一個表單,通過javascript添加了一個複選框,當表單被提交時,它檢查複選框是否被勾選。這在Firefox或Chrome中運行良好,但在IE7或8中會導致錯誤document.myform.mycheckbox.checked爲空或不是對象。Javascript對象爲空或不在IE中的對象

var checkbox = document.createElement("input"); 
checkbox.type = "checkbox"; 
checkbox.name = "mycheckbox"; 
checkbox.value= 291; 
var div = document.getElementById("addcb"); 
div.appendChild(checkbox); 
checkbox.checked = false; 

在形式標籤我的onSubmit =「返回CheckForm();」,其工作在Firefox或Chrome確定,但在IE7或8它提交表單,而不檢查的形式,或者其它形式的對象。

if (document.myform.mycheckbox.checked == false){ 
    errorMsg += "\n\tAgree \t- Please Click I Agree Checkbox"; 
} 

//If there is aproblem with the form then display an error 
if ((errorMsg != "") || (errorMsgLong != "")){ 
    msg = "_______________________________________________________________\n\n"; 
    msg += "The form has not been submitted because there are problem(s) with the form.\n"; 
    msg += "Please correct the problem(s) and re-submit the form.\n"; 
    msg += "_______________________________________________________________\n\n"; 
    msg += "The following field(s) need to be corrected: -\n"; 

    errorMsg += alert(msg + errorMsg + "\n" + errorMsgLong); 
    return false; 
} 

return true; 

我用的開發工具來創建一個Brakpoint該報告錯誤:在創建它

document.myform.mycheckbox.checked is null or not an object 

回答

0

,給它一個ID以及

checkbox.id = "mycheckbox"; 

,然後找到它做一個

if (document.getElementById("mycheckbox").checked == false) 
+0

仍返回爲空或不是一個對象在IE7/8中提交表單 – user2895984

+0

document.getElementById(「mycheckbox」)。getAttribute('checked')'? –

+0

啊,成功了。感謝那。 – user2895984

相關問題