检查是否在IF语句中选中了复选框

时间:2012-11-17 19:16:57

标签: javascript html javascript-events

如何检查IF语句中是否检查了文本框。在我的示例中,我检查是否选中了“否”和“ely”以及复选框3或4,然后勾选复选框2。

JavaScript代码:

if(likeShield.value == "noShield" && chooseShield.value == "ely" &&  
            (document.getElementById("cb3").checked == true ||
             document.getElementById("cb4").checked == true))
{
    document.getElementById("cb2").checked=true;
}

上述代码不起作用。我该如何解决这个问题?

HTML代码:

<html>
<head>
<script type="text/javascript" src="myJavascript.js"></script>
</head>
<body>

<select id="likeShield" onchange="showTicks()">
   <option value="select1">Select</option>
   <option value="yesShield">Yes</option>
   <option value="noShield">No</option>
</select>

<select id="chooseShield" onchange="showTicks()">
   <option value="select1">Select</option>
   <option value="arc">Arcane</option>
   <option value="ely">Elysian</option>
<option value="spec">Spectral</option>
<option value="anylist">Choose any</option>
</select>

<table border = "1">
<tr>
   <th> tickbox </th>
   <th> shield parts </th>
   <th> description </th>
   <th> cost </th>
</tr>
<tr>
   <td><input type="checkbox" id="cb1"></td>
   <td> arc sigil </td>
   <td> Large magic part </td>
   <td> 5m </td>
</tr>
<tr>
   <td><input type="checkbox" id="cb2"></td>
   <td> arc shield </td>
   <td> A extremely powerful magic shield </td>
   <td> 60m </td>
</tr>
<tr>
   <td><input type="checkbox" id="cb3"></td>
   <td> arc special item </td>
   <td> special element </td>
   <td> 10m </td>
</tr>
<tr>
   <td><input type="checkbox" id="cb4"></td>
   <td> elysian sigil  </td>
   <td> A sigil found by dragons </td>
   <td> 50m </td>
</tr>   
<tr>
   <td><input type="checkbox" id="cb5"></td>
   <td> elysian shield </td>
   <td> A extremely powerful ranging shield </td>
   <td> 40m </td>
</tr>   
<tr>
   <td><input type="checkbox" id="cb6"></td>
   <td> elysian special item </td>
   <td> A special attack attached to shield </td>
   <td> 25m </td>
</tr>
<tr>
   <td><input type="checkbox" id="cb7"></td>
   <td> spectral sigil  </td>
   <td> easily obtainable from goblins </td>
   <td> 4m </td>
</tr>
<tr>
   <td><input type="checkbox" id="cb8"></td>
   <td> spectral shield </td>
   <td> Impressive stats </td>
   <td> 15m </td>
</tr>
<tr>
   <td><input type="checkbox" id="cb9"></td>
   <td> spectral special item </td>
   <td> Does double damage </td>
   <td> 30m </td>
</tr>
</table>
</body>
</html> 

2 个答案:

答案 0 :(得分:0)

document.getElementById("checkBox2").checked=true;

您的代码中没有这样的元素。

也许你的意思是:

document.getElementById("cb2").checked=true;

如果您对错误所做的事情进行进一步探索,则需要 testcase ,以显示您执行条件语句的完整上下文。目前,它还不清楚它是在事件处理程序中,还是在函数调用中,或者是什么......

答案 1 :(得分:0)

如果你想要的是更新复选框时更新复选框,你必须听取更改事件:

document.onload = function(){
var likeShield = document.getElementById("likeShield");
var chooseShield = document.getElementById("chooseShield");
var cb3 = document.getElementById("cb3");
var cb4 = document.getElementById("cb4");
function checkchecks() {
    if (
        likeShield.value=="noShield" && chooseShield.value == "ely"
        && (cb3.checked || cb4.checked)
    ) {
        document.getElementById("cb2").checked=true;
    }
}
likeShield.onchange=checkchecks;
chooseShield.onchange=checkchecks;
cb3.onchange=checkchecks;
cb4.onchange=checkchecks;
}

Demonstration