<form id="form2" name="form2" action="">
<div class="form_colour" id="formcolour"><img src="../Images/red_car.jpg" alt="" width="370" height="124" />This car is
<label for="textarea"></label>
<input name="text_1" type="text" class="form_colour" id="text_1" />
</div>
<p> </p>
<p><img src="../Images/green_car.jpg" width="370" height="124" />This car is
<input name="text_2" type="text" class="form_colour" id="text_2" />
</p>
<p>
<img src="../Images/Yellow_car.jpg" width="370" height="124" />This car is
<input name="text_3" type="text" class="form_colour" id="text_3" maxlength="8" />
</p>
<p>
<input name="confirm" type="button" class="form_colour" id="confirm" value="Confirm" />
</p>
</form>
我目前对于包含3个文本字段的表单有以下代码(表单的代码在此之上,Javascript代码在此文本下面)。
我的目标是根据文本值的输入,用户将获得一条消息并留在页面上,或者如果它们是正确的,则获取消息并转到另一页面。我之前为下拉列表做了这个。我所做的只是将变量从下拉列表名称更改为文本字段名称。现在,当我运行这个,没有任何反应,我还需要做更多的事情吗?
</script>
<script type="text/javascript">
function checkValues() { // this line must be within the script tags
var a = document.form2.text_1.value;
var b = document.form2.text_2.value;
var c = document.form2.text_3.value;
if (a == "red" && b == "green" && c == "yellow"){
alert ("Correct you have won press OK for your Reward!")
window.location.href = "Reward.html"; // redirect to new page
}
else {
alert ("Not right Please try again!");
window.location.href = "Money_Match.html";
}
}
</script>
</script>
答案 0 :(得分:0)
您没有按下按钮调用脚本,请查看http://jsfiddle.net/3CgcH/7/
中的工作示例您需要为按钮指定onclick处理程序,该处理程序将调用checkValues
函数
<input name="confirm" type="button" class="form_colour" id="confirm" value="Confirm" onclick="checkValues();" />
更合适的方法是使用addEventListener
和attachEvent
在html元素上注册事件