我有一个带有单选按钮的无序列表。我想获取选中的单选按钮的 ID。
这是HTML:
<ul class="class1">
<li>
<label id="id1">
<input id="id1A" type="radio" value="2" name="name1" onchange="say(); return false;">
<span class="className1">Some Text1</span>
</label>
</li>
<li>
<label id="id2">
<input id="id2A" type="radio" value="1" name="name1" onchange="say(); return false;">
<span class="className1">Some Text2</span>
</label>
</li>
</ul>
<ul class="class2">
<li>
<label id="id21">
<input id="id21A" type="radio" value="2" name="name2" onchange="say(); return false;">
<span class="className21">Some Text21</span>
</label>
</li>
<li>
<label id="id2">
<input id="id22A" type="radio" value="1" name="name2" onchange="say(); return false;">
<span class="className21">Some Text22</span>
</label>
</li>
</ul>
这是Javascript:
<script type="text/javascript">
function say() {
var firstChild = document.getElementsByClassName('class1');
firstChild = Array.prototype.slice.call(firstChild);
for(var j =0; j < parent.length; j++){
var firstChildElem = firstChild;
for (var x = 0; x < secondChild.length; x ++) {
if (secondChild[x].checked) {
alert("You checked " + secondChild[w]);
}
}
}
</script>
但是这个脚本不起作用。