我有多組單選按鈕,我想單擊時改變顏色。我使用name
來改變顏色,但是當所有單選按鈕的名稱相似時,它被計爲一組。我需要把它們分開。如何通過點擊單選按鈕改變顏色,但使用值
我的代碼:
function getInputValue() {
var radioObj = document.forms["form"]["tt"];
for (var i=0; i<radioObj.length; i++) {
if (radioObj[i].checked)
return radioObj[i].value;
}
alert("No radio button was selected.")
}
function change_it(id) {
var a = getInputValue();
if(a=="Bad") {
document.getElementById(id).style.backgroundColor = "#FFBFBF";
return false;
} else if(a=="Good") {
document.getElementById(id).style.backgroundColor = "#F0FFF0";
return false;
}
}
<table>
<tr id="<?php echo $row['id'];?>">
<td>
It changes the color in here.
</td>
</tr>
</table>
<form action="#" method="get" name="form">
Group 1
<input type="radio" name="tt" id="1" onclick="change_it();" value="Bad" />Bad </input>
<input type="radio" name="tt" id="1" onclick="change_it();" value="Good" />Good</input>
Group 2
<input type="radio" name="tt" id="2" onclick="change_it();" value="Bad" />Bad </input>
<input type="radio" name="tt" id="2" onclick="change_it();" value="Good" />Good</input>
</form>
我porblem是,如果我改變name
顏色不會改變了,如果我保持name
一樣,那麼所有4個計爲一個組。
編輯 我想說清楚,我的代碼已完美工作。我試圖弄清楚的是如何讓顏色變化不會被卡在name
屬性中。
你有'document.forms [「form」] [「tt」]'hardcoded英寸,這也需要是動態的,如果你想分組他們 – Ian 2013-04-23 14:31:39
你也給** 5 **東西相同的'id'。所有'id's必須是唯一的 – Ian 2013-04-23 14:32:29
和''應該是'' – Ian 2013-04-23 14:32:56