[英]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? 我究竟做错了什么?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.