所以我一直在为一个类项目的简单脚本。我是一个新的JavaScript学生,所以我给了我一个很难的时间,即使我认为它可以轻松解决。我们提交了表格,我输入了7并得到了“错误答案”,这很好,但是我输入了8(正确答案),并且仍然输出错误的答案。我不知道为什么它不能从8区分其他任何数量的请帮忙JQuery的提交功能没有验证
$("#story").submit(function (e) {
var answer = document.getElementById('human-story');
if (answer != 8) {
console.log(answer);
e.preventDefault();
}else {
alert("right answer");
e.preventDefault();
}
})
<form class="form-horizontal contact" name="contact" method="post" action="" id="story">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4>We are excited to hear your story!</h4>
</div>
<div class="modal-body">
<div class="form-group">
<label for="contact-name" class="col-lg-2 control-label">Name:</label>
<div class="col-lg-10">
<input type="text" name="first_name" class="form-control" id="contact-name" placeholder="First & Last Name" required="" >
</div>
</div>
<div class="form-group">
<label for="contact-email" class="col-lg-2 control-label">Email:</label>
<div class="col-lg-10">
<input type="email" name="email" class="form-control" id="contact-email" placeholder="[email protected]" required="">
</div>
</div>
<div class="form-group">
<label for="contact-message" class="col-lg-2 control-label">Story:</label>
<div class="col-lg-10">
<textarea name="message" rows="8" class="form-control" style="resize:none;" required=""></textarea>
</div>
</div>
<div class="form-group">
<label for="human" class="col-sm-2 control-label">5 + 3 = ?</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="human-story" name="human-story" placeholder="Your Answer" required="">
</div>
</div>
</div>
<div class="modal-footer">
<a href="#" class="btn btn-default" data-dismiss="modal">Close</a>
<button class="btn btn-success" name="submit-story" type="submit-story" value="Send!">Send</button>
</div>
</form>
检查每次提交答案的价值!提醒它并看看它给了什么! – 2015-02-06 16:53:58
为什么不使用:'if($(“。form-control”).val()=='8'){...} else {...}'? – 2015-02-06 16:54:45
为什么要解析为一个整数?为什么不简单评估它是一个字符串? – user3479671 2015-02-06 16:56:02