1

我有一个通过javascript添加复选框的表单,当提交表单时,它会检查复选框是否已被勾选。这在 Firefox 或 Chrome 中运行良好,但在 IE7 或 8 中会导致错误 document.myform.mycheckbox.checked is null or not an object。

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="return 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
4

1 回答 1

0

在创建它时,也给它一个 ID

checkbox.id = "mycheckbox";

然后找到它做一个

if (document.getElementById("mycheckbox").checked == false) 
于 2013-10-18T19:05:46.757 回答