简体   繁体   English

未捕获的TypeError:无法使用for循环将属性'innerHTML'设置为null

[英]Uncaught TypeError: Cannot set property 'innerHTML' of null using a for loop

Basically what I have for this code is I want to take the number of pets a user inputs, take down the names of the pets, and put them in a variable that I can print out later in the program. 基本上,我对这段代码的了解是,我想获取用户输入的宠物数量,记下宠物的名称,然后将它们放入一个变量中,以便稍后在程序中打印出来。

{
return document.getElementById(id);
}
var processInfo = function ()
{
    //Setting varibles from HTML
    first = $('firstname');
    last = $('lastname');
    petnum = $('numpets');
    //For loop for pet names
    var Petlist = '';
    for (cntr = 1; cntr <= petnum.value; cntr++)
    {
        petID = 'pet' + cntr;
        PetName = $(petID).value;
        Petlist += PetName;
    }

    //Checking for blank strings to be filled
    /*var errorFoundFlag = 'N';

    if (something is found to have an error)
    {
        msg += "error found with something";
        errorFoundFlag = 'Y';
    }

    if (somethingElse is found to have an error)
    {
        msg += "error found with somethingElse ";
        errorFoundFlag = 'Y';
    }

    if (errorFoundFlag == 'N')
    {
       Do the processing called for in the "Success" section above
    }
    */



    $("msg").innerHTML = Petlist;

}

window.onload = function ()
{
$("mybutton").onclick = processInfo;
}
<p>How Many Pets do you have?  (0-3): <input type="text" id="numpets" size="1" maxlength="1">
<span id="numpets_error"></span>
</p>

<p>List your Pet's names:
    <input type="text" id="pet1">
    <input type="text" id="pet2">
    <input type="text" id="pet3">
</p>

<p><input id="mybutton" type="button" value="Submit Information"></p>

<p id="message"></p>

I want the for loop to take ids="pet1" "pet2" and "pet3" and put them together as a string into a variable called Petlist . 我希望for循环采用ids =“ pet1”“ pet2”和“ pet3”并将它们作为字符串放入一个名为Petlist的变量中。 I want to set inner.HTML to petlist , but I am getting the following error: 我想将inner.HTML设置为petlist ,但是出现以下错误:

Uncaught TypeError: Cannot set property 'innerHTML' of null on line 43 Uncaught TypeError:无法在第43行上将属性'innerHTML'设置为null

where I call $("msg").innerHTML = Petlist 我称之为$("msg").innerHTML = Petlist

What am I doing wrong? 我究竟做错了什么?

You are combining DOM with jQuery . 您正在将DOMjQuery结合在一起。 That won't work. 那行不通。
Use the html method which is the jQuery equivalent of innerHTML . 使用html方法,它是jQuery等效于innerHTML

$("msg").html(Petlist);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的TypeError:无法将属性&#39;innerHTML&#39;设置为null - Uncaught TypeError: Cannot set property 'innerHTML' of null 未捕获的Typeerror:无法将属性innerHTML设置为null - Uncaught Typeerror: Cannot set property innerHTML of null 未捕获的TypeError:无法将属性&#39;innerHTML&#39;设置为null - Uncaught TypeError: Cannot set property 'innerHTML' of null 未捕获的TypeError:无法将属性&#39;innerHTML&#39;设置为null - Uncaught TypeError: Cannot set property 'innerHTML' of null 未捕获的TypeError:无法设置null的属性&#39;innerHTML&#39; - Uncaught TypeError: Cannot set property 'innerHTML' of null 未捕获的TypeError:无法在phonegap中将属性&#39;innerHTML&#39;设置为null - Uncaught TypeError: Cannot set property 'innerHTML' of null in phonegap 未捕获的TypeError:无法在第34行中将属性&#39;innerHTML&#39;设置为null - Uncaught TypeError: Cannot set property 'innerHTML' of null in line 34 Uncaught TypeError:无法在JS创建的div上将属性&#39;innerHTML&#39;设置为null - Uncaught TypeError: Cannot set property 'innerHTML' of null on JS created div 未被捕获的TypeError:无法将属性&#39;innerHTML&#39;设置为null [VueJs] - Uncaught TypeError: Cannot set property 'innerHTML' of null [VueJs] 未捕获(承诺)TypeError:无法将属性&#39;innerHTML&#39;设置为null - Uncaught (in promise) TypeError: Cannot set property 'innerHTML' of null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM