Javascript innerhtml的复选框 [英] Javascript innerhtml of checkbox
问题描述
我有一些复选框id ='0','1','2','3'等等。所以我需要得到文本的复选框。这里是PHP中的复选框代码。
Well I have some checkboxes with id='0','1','2','3' etc. So I need to get text of checkboxes. Here is code of checkboxes in PHP.
'<input id="'.$i.'"type="checkbox" name="option1" value="a1" onclick="checkYears()">'.$years[$i].'</input>
所以我需要得到$ years [$ i]
我使用这个代码。
So i need to get $years[$i]-value. I'm using this code.
while (bool==false)
{
if (document.getElementById(i)!=null)
{
if (document.getElementById(i).checked)
{
years.push(1);
yearsValue.push(document.getElementById(i).innerHTML);
document.getElementById(i).innerText="WORKS";
console.log(yearsValue[i]);
}
else
{
years.push(0);
yearsValue.push(document.getElementById(i)).innerHTML);
console.log(yearsValue[i]);
}
}
else
{
bool=true;
}
i++;
}
0和1添加确定,但是当我试图使用innerHTML
0's and 1's are added ok, but when i'm trying to use innerHTML there is and undefined values in consle.
有没有建议?
推荐答案
InnerHTML获取元素的HTML 内容。即开始标记和结束标记之间的数据。输入是空元素,它们不能有内容(在HTML中(与XHTML相对),它们也不能有结束标记)。您的HTML无效,您应该使用验证器。
InnerHTML gets the HTML content of an element. i.e. the data between the start tag and the end tag. Inputs are empty elements, they cannot have content (in HTML (as opposed to XHTML) they cannot have an end tag either). Your HTML is invalid and you should use a validator.
你可能想要这样的东西:
You probably want something like this:
<label>
<input type="checkbox">
<span>Text label</span>
</label>
然后你可以得到:
checkbox.parentNode.innerHTML;
或
checkbox.parentNode.getElementsByTagName('span')[0].innerHTML
这篇关于Javascript innerhtml的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!