我的代码无法正常工作,当我尝试从单选按钮和下拉菜单中获取值时,JS 函数出现问题。
谁能告诉我怎么了?
CSS:
#mytable {
width:400px;
border: 1pt solid black;
}
#mytable tr {
height:50px;
}
#mytable td {
width:20%;
border: 1px solid black;
}
Javascript:
function colorit(){
var letter;
if(document.getElementsByName("plusSign").checked) letter = "+";
else if(document.getElementsByName("letterX").checked) letter = "X";
else if(document.getElementsByName("letterH").checked) letter = "H";
var colorList = document.getElementsByName("color");
var x = document.getElementById('mytable').getElementsByTagName('td');
for(i=0;i<x.length;i++) {
x[i].style.backgroundColor = colorList.options[colorList.selectedIndex].text;
x[i].innerHTML = letter;
}
}
function clearit(){
var x = document.getElementById('mytable').getElementsByTagName('td');
for(i=0;i<x.length;i++) {
x[i].style.backgroundColor = "";
x[i].innerHTML = "";
}
}
HTML:
<form name="frm1">
<table>
<tr>
<td>Pattern Choice:</td>
<td>
<input type="radio" name="plusSign" value="PlusSign" />Plus Sign</td>
<td>
<input type="radio" name="letterX" value="LetterX" />Letter X</td>
<td>
<input type="radio" name="letterH" value="LetterH" />Letter H</td>
</tr>
<tr>
<td>Color Choice:</td>
<td>
<select name="color">
<option>Red</option>
<option>Blue</option>
<option>Yellow</option>
<option>Green</option>
<option>Orange</option>
</select>
</td>
</tr>
<table id="mytable">
<TR>
<TD></TD>
<TD></TD>
<TD></TD>
<TD></TD>
<TD></TD>
</TR>
<TR>
<TD></TD>
<TD></TD>
<TD></TD>
<TD></TD>
<TD></TD>
</TR>
<TR>
<TD></TD>
<TD></TD>
<TD></TD>
<TD></TD>
<TD></TD>
</TR>
<TR>
<TD></TD>
<TD></TD>
<TD></TD>
<TD></TD>
<TD></TD>
</TR>
<TR>
<TD></TD>
<TD></TD>
<TD></TD>
<TD></TD>
<TD></TD>
</TR>
</TABLE>
<tr>
<input type="button" value="Color It" onclick="colorit()" />
<input type="button" value="Clear" onclick="clearit()" />
</tr>
</table>
</Form>