我有一个需要验证的表单,我还需要在不刷新页面的情况下提交它。我能够获得一个表单来验证和提交,但页面刷新了。然后我能够获得一个无需提交的表单,但我无法进行验证。
我在这里查看了大量的教程和帖子,我觉得这是正确的整体格式......我只是确定我错过了一些东西。
任何帮助将不胜感激!
这是我的脚本:
$(document).ready(function(){
$("#addstudent").validate({
debug: false,
rules: {
studentid: "required",
teacher: "required",
assignment: "required",
date: "required",
},
messages: {
studentid: "Please enter the student's ID number.",
teacher: "Please enter your name.",
assignment: "Please select a tutoring assignment.",
date: "Please select a day.",
},
submitHandler: function(form) {
$.ajax({
url: 'add.php',
type: 'POST',
data: $("form.addstudent").serialize(),
success: function() {
$("#studentid").val(""), $('#studentid').focus(), $('#results').load('addresults.php', function(){
});
}
});
return false;
}
});
});
这是我的表单的代码:
<form name="addstudent" id="addstudent" action="" method="post">
<fieldset><legend>Add student to tutoring list</legend>
<div><label for="studentid">ID number</label><input type="text" name="studentid" id="studentid"></div>
<div><label for="day">Date</label><select name="date" id="date">
<option value="">Please select a day</option>
<option value="mon">Monday <? echo $monday; ?></option>
<option value="tue">Tuesday <? echo $tuesday; ?></option>
<option value="wed">Wednesday <? echo $wednesday; ?></option>
<option value="thu">Thursday <? echo $thursday; ?></option>
<option value="fri">Friday <? echo $friday; ?></option>
</select></div>
<div><label for="assignment">Tutoring assignment</label><select name="assignment" id="assignment">
<option value="">Please select an assignment</option>
<option value="att">Activity Time</option>
<option value="acc">ACC</option>
<option value="tech">ACC Tech </option>
<option value="ast">After School</option>
</select></div>
<div><label for="teacher">Assigning teacher</label><input type="text" name="teacher" id="teacher"></div>
<input type="submit" name="submit" value="submit">
</fieldset>
</form>
编辑:第一次编辑(现已删除)极大地改变了问题。对造成的任何挫折表示歉意。下面选择的答案本身就解决了这种形式的问题。