【发布时间】:2014-04-03 16:34:15
【问题描述】:
我想问一些棘手的 javascript,这是关于 if/else if/else 的问题。 我想对“是”或“否”提出问题,这就是逻辑:如果问题 1 是“是”,问题 2 是“是”,问题 3 是“否”,结果是“好”,如果问题 1 是“是”问题 2 是“否”,问题 3 是“是”,结果是“不好”。我正在为这种情况编写代码但无法正常工作,这是我的代码,我在 html 中使用复选框来选择答案:
javascript
<script type="text/javascript">
var question1 = document.getElementById("a");
question2 = document.getElementById("b");
question3 = document.getElementById("c");
answer1 = document.getElementById("d");
answer2 = document.getElementById("e");
answer3 = document.getElementById("f");
answer4 = document.getElementById("g");
answer5 = document.getElementById("h");
answer6 = document.getElementById("i");
function TheResult(form){
if(question1 == answer1 && question2 == answer3 && question3 == answer6 ){
document.write('good');
}else if(question1 == answer1 && question2 == answer4 && question3 == answer5 ){
document.write('not good');
}else {
document.write('ERROR');
}
}
html
<form>
<p id = "a"><b>Question1</b></p>
<input type="checkbox" name="a1" value="YES" id = "d">YES
<input type="checkbox" name="a1" value="NO" id = "e">NO<br><br>
<p id = "b"><b>Question2</b></p>
<input type="checkbox" name="a2" value="YES" id = "f" >YES
<input type="checkbox" name="a2" value="NO" id = "g" >NO<br><br>
<p id = "c"><b>Question3</b></p>
<input type="checkbox" name="a3" value="YES" id = "h">YES
<input type="checkbox" name="a3" value="NO" id = "i">NO<br><br>
<input type="button" name="Submit" value="Result" style="margin-left:100px;" onClick="TheResult(this.form)" >
</form>
我的代码在任何情况下总是打印出“好”,请帮忙。
【问题讨论】:
-
你在比较元素???
-
比较元素...呵呵!应该是
question2.value... so on....... -
你希望
question1 == answer1做什么? -
我想你可能正在寻找
.value... -
您是在比较元素,而不是它们的值。无论如何,question1(例如)除了作为一个对象之外没有任何价值。
标签: javascript