document.getElementById("items_" + i) 为 null

发布于 2024-11-27 15:26:14 字数 562 浏览 0 评论 0 原文

我有以下代码:

<script type="text/javascript">
for(var i=1; i<=3; i++) {
    document.getElementById("items_"+i).checked=true;
}
</script>                   

使用以下 HTML,

<input type="checkbox" id="items_1" name="myitems" value="1" />
<input type="checkbox" id="items_2" name="myitems" value="2" />
<input type="checkbox" id="items_3" name="myitems" value="3" />

我收到一条错误消息:

document.getElementById("items_" + i) is null.

如何解决此问题?

I have the following code:

<script type="text/javascript">
for(var i=1; i<=3; i++) {
    document.getElementById("items_"+i).checked=true;
}
</script>                   

With the following HTML

<input type="checkbox" id="items_1" name="myitems" value="1" />
<input type="checkbox" id="items_2" name="myitems" value="2" />
<input type="checkbox" id="items_3" name="myitems" value="3" />

I get an error saying:

document.getElementById("items_" + i) is null.

How do I fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

﹂绝世的画 2024-12-04 15:26:14

for 循环中的第一个索引将为零。

您是否有 id 为 items_0 的元素?

这可能就是导致您出现问题的原因。将 i 的初始值设置为 1

其次,您要确保 Javascript 代码在 DOM 加载后执行。

我建议您考虑使用 jQuery 因为这使得做这些事情变得更加简单。

The first index in your for loop will be zero.

Do you have an element with the id items_0 ?

That's likely what's causing your problem. Set the initial value of i to 1

Secondly, you want to make sure that your Javascript code executes after the DOM has loaded.

I recomend you look into using jQuery as this makes it so much simpler to do these kinds of things.

我家小可爱 2024-12-04 15:26:14

好吧,您的代码看起来像是第一次迭代指向 i = 0,因此它试图找到 id 为“items_0”的输入标签。

这是一个 JSBin,显示代码正常工作:

http://jsbin.com/amonel/edit

Well your code looks like it's first iteration is pointed to i = 0 so it's trying to find an input tag with the id of 'items_0'.

Here is a JSBin that shows the code working correctly:

http://jsbin.com/amonel/edit

如此安好 2024-12-04 15:26:14
var VanillaRunOnDomReady = function() {
    // code to execute after DOM has loaded  
    for (var i = 1; i <= 3; i++) {        
        document.getElementById("items_" + i).checked = true;
    }  
}

var alreadyrunflag = 0;
if (document.addEventListener) {    
    document.addEventListener("DOMContentLoaded", function(){
        alreadyrunflag=1;
        VanillaRunOnDomReady();
    }, false);
}
else if (document.all && !window.opera) {
    document.write('<script type="text/javascript" id="contentloadtag" defer="defer" src="javascript:void(0)"><\/script>');

    var contentloadtag = document.getElementById("contentloadtag");

    contentloadtag.onreadystatechange=function(){
        if (this.readyState=="complete"){
            alreadyrunflag=1;
            VanillaRunOnDomReady();
        }
    }
}

window.onload = function(){
  setTimeout("if (!alreadyrunflag){VanillaRunOnDomReady}", 0);
}
var VanillaRunOnDomReady = function() {
    // code to execute after DOM has loaded  
    for (var i = 1; i <= 3; i++) {        
        document.getElementById("items_" + i).checked = true;
    }  
}

var alreadyrunflag = 0;
if (document.addEventListener) {    
    document.addEventListener("DOMContentLoaded", function(){
        alreadyrunflag=1;
        VanillaRunOnDomReady();
    }, false);
}
else if (document.all && !window.opera) {
    document.write('<script type="text/javascript" id="contentloadtag" defer="defer" src="javascript:void(0)"><\/script>');

    var contentloadtag = document.getElementById("contentloadtag");

    contentloadtag.onreadystatechange=function(){
        if (this.readyState=="complete"){
            alreadyrunflag=1;
            VanillaRunOnDomReady();
        }
    }
}

window.onload = function(){
  setTimeout("if (!alreadyrunflag){VanillaRunOnDomReady}", 0);
}
感悟人生的甜 2024-12-04 15:26:14

我最终使用了:

$('#items_'+i).attr("checked", true);

I ended up using:

$('#items_'+i).attr("checked", true);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文