2012-11-14 116 views
1

我想链接我的HTML下拉菜单到我的表中有复选框。JavaScript链接下拉菜单复选框

此javascript函数试图看到如果“弧”值已被选择,则禁用该复选框3,其示出了光谱:

function showShields() 
{ 
if (document.getElementById("Shield").value == ("arc") 
{ 
document.getElementById("checkBox3").disabled=true; 
} 
} 

这是HTML代码:

<!DOCTYPE html> 

<html> 
<head> 
<script src="SpiritShield.js"></script> 
</head> 
<body> 
<p>What kind of shield are you interested in?</p> 
<select id="Shield"> 
    <option value="arc">ARC</option> 
    <option value="divine">GAS</option> 
    <option value="spectral">MIG</option> 
    <option value="elysian">ANY</option> 
</select> 
</body> 
<table border="2" cellspacing="0"> 
<tr> 
    <th>Checkbox</th> 
    <th>Shield</th> 
</tr> 
<tr> 
    <td><input type="checkbox" id="checkBox1"</td> 
    <td>ARC1</td> 
</tr> 
<tr> 
    <td><input type="checkbox" id="checkBox2"</td> 
    <td>ARC2</td> 
</tr> 
<tr> 
    <td><input type="checkbox" id="checkBox3"</td> 
    <td>SPECTRAL1</td> 
</tr> 
<tr> 
    <td><input type="checkbox" id="checkBox4"</td> 
<td>SPECTRAL2</td> 
</tr> 
</table> 

</html> 
莫非

有人帮我解决这个问题?

谢谢

+0

在HTML是无效的,有一些未封闭的标签。 –

+0

嗯在哪里?另外我不认为这就是为什么我的javascript功能没有工作的原因:( – Gary

+0

Muthu是正确的,你的/ body标签是你选择后,你应该把它移动到/ html之前的底部 – Sean

回答

1

你的代码是正确的,虽然我没有注意到如果条件丢失的圆括号:

function showShields() 
{ 
if (document.getElementById("Shield").value == ("arc")) //your missing missing parenthesis was here 
{ 
document.getElementById("checkBox3").disabled=true; 
} 
} 
+0

嗨,感谢您的回复。上述仍然不幸的工作。当我从下拉菜单中选择ARC时,表格中的复选框未被禁用。 :( – Gary

1

试试这个:http://jsfiddle.net/Hpcsq/2/

function showShields(what) 
    { 
    if (what.value == ("arc")) 
     { 
     document.getElementById("checkBox3").checked=false; 
     } 
    }​ 

<!DOCTYPE html> 

<!DOCTYPE html> 

<html> 
<head> 
<script src="SpiritShield.js"></script> 
</head> 
<body> 
<p>What kind of shield are you interested in?</p> 
<select id="Shield" onchange="showShields(this)"> 
     <option value="arc">---</option> 
    <option value="arc">ARC</option> 
    <option value="divine">GAS</option> 
    <option value="spectral">MIG</option> 
    <option value="elysian">ANY</option> 
</select> 
</body> 
<table border="2" cellspacing="0"> 
<tr> 
    <th>Checkbox</th> 
    <th>Shield</th> 
</tr> 
<tr> 
    <td><input type="checkbox" id="checkBox1"</td> 
    <td>ARC1</td> 
</tr> 
<tr> 
    <td><input type="checkbox" id="checkBox2"</td> 
    <td>ARC2</td> 
</tr> 
<tr> 
    <td><input type="checkbox" id="checkBox3" checked="checked"></td> 
    <td>SPECTRAL1</td> 
</tr> 
<tr> 
    <td><input type="checkbox" id="checkBox4"</td> 
<td>SPECTRAL2</td> 
</tr> 
</table> 

</html>​ 
+0

嗨,感谢您的回复,该代码只是勾选了方框,我希望它能禁用复选框,以便用户无法点击。 – Gary

+0

我已更新代码,再次检查。错了;-) – Tschallacka

+0

好的谢谢,但它不工作大声笑。你可以用js试试这个代码来看看它是否有效。所以,当弧选项被选中时,表格应该自动禁用复选框3. Ta – Gary