2013-10-18 40 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

相关问题