gpt4 book ai didi

Javascript 对象在 IE 中为 null 或不是对象

转载 作者:行者123 更新时间:2023-11-28 08:48:25 26 4
gpt4 key购买 nike

我有一个由javascript添加了复选框的表单,提交表单时它会检查该复选框是否已被选中。这在 Firefox 或 Chrome 中工作正常,但在 IE7 或 8 中会导致错误 document.myform.mycheckbox.checked 为 null 或不是对象。

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

最佳答案

创建它时,也给它一个 ID

checkbox.id = "mycheckbox";

然后找到它执行

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

关于Javascript 对象在 IE 中为 null 或不是对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19457132/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com