在IF语句中检查复选框

时间:2012-11-17 17:27:32

标签: javascript html javascript-events

我目前无法使我的IF语句正常工作我希望用户选择屏蔽" ely"然后选择文本框3或4将自动勾选文本框2。

这是我的JavaScript代码:

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

这可能是错误的,因为它不起作用,所以我怎么能检查用户是否选择了文本框3或4。

这是我的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="showShield" 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> 

7 个答案:

答案 0 :(得分:2)

if(showShield.value == "ely" && (document.getElementById("cb3").checked || document.getElementById("cb4").checked))//comparison
{
    document.getElementById("cb2").checked=true; //asignment
}
  1. Operator Precedence: &&||之前完成,因此您需要括号
  2. ==,而非=进行比较(最好===不会自动转换类型并进行严格比较)
  3. ;语句终止符,不应在其中间使用。

答案 1 :(得分:1)

if(showShield.value === "ely" && (document.getElementById("cb3").checked == true || document.getElementById("cb4").checked == true))

无需使用分号并使用==而不是=。条件也需要括号。

答案 2 :(得分:0)

试试这个

if(ely.value == "ely" && (document.getElementById("checkBox3").checked ||
  document.getElementById("checkBox4").checked))
{
    document.getElementById("checkBox2").checked=true;
}

答案 3 :(得分:0)

您可能想要:

if(ely.value == ("ely") && document.getElementById("checkBox3").checked == true || ely.value == ("ely") && document.getElementById("checkBox4").checked == true)
{
document.getElementById("checkBox2").checked=true;
    }

if(ely.value == ("ely") && (document.getElementById("checkBox3").checked == true || document.getElementById("checkBox4").checked == true))
{
document.getElementById("checkBox2").checked=true;
    }

这是因为逻辑运算符具有它们的比较顺序,就像算术乘法优先于加法一样。嗯,我是JS的新手,但我相信这是它在各地的运作方式。此外,您必须使用双等号进行比较,因为single用于分配值。

答案 4 :(得分:0)

这里可能有点错误(因为你这里没有提供太多信息),但至少,而不是

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

你应该

document.getElementById("checkBox3").checked == true

“checkBox4”也是如此。

以下是对原因的快速解释:

1)当您使用一个等号时,您设置一个值。因此,不是测试是否选中checkBox3,而是基本上告诉我checkBox3被检查。 if语句需要使用两个等号,因为它使语句成为问题而不是命令。

2)只有在声明结束时才需要分号。由于“if”行是一个完整的语句,因此您不需要在其中使用分号。

希望有意义!

答案 5 :(得分:0)

if(showShield.value == ("ely") && document.getElementById("cb3").checked = true; || document.getElementById("cb4").checked = true;)

showShield.value == ("ely") && document.getElementById("cb3").checked = true; js检查了条件,因为showShield.value必须ely并且由于运算符优先级而检查cb3然后检查了'OR'语句{{1} }已经过检查。

我们可以解决这个问题。

cb4

答案 6 :(得分:0)

javascript复选框if else条件

$checkbox = document.getElementById("checkBoxId").checked;

if($checkbox == true) {
    //check true
} else {
    //check false
}

jQuery复选框if else条件

$checkbox = $("#checkBoxid").is(":checked");

if($checkbox == true) {
     //check true
} else {
     //check false
}
相关问题