我是 ajax 和 jquery 的新手。我已使用以下代码提交表单数据,它工作正常。
但我需要在提交表单后重置(清除表单字段),所有验证都为真。在哪里编写代码以清除表单。
html表单:
<form action="#" name="formdata" id="replyform" method="post">
<div class="pass-form">
<div class="input-row">
<div class="row_label">
Old Password
</div>
<span><input type="text" id="User_pass1" name="User_pass1"/></span>
</div>
<div class="input-row">
<div class="row_label">
New Password:
</div>
<span><input id="User_pass2" name="User_pass2" type="password"/></span>
</div>
<div class="input-row">
<div class="row_label">
Re-Enter New Password:
</div>
<span><input id="User_pass3" name="User_pass3" type="password"/></span>
</div>
<div class="input-row">
<div class="row_label">
</div>
<span> <button name="chpass" onclick="submitform();" class="button" type="button" value="Save">Save</button>
<button name="chpr" type="reset" class="button" value="Reset">Reset</button></span>
</div>
</div>
</form>
Javascript函数:
function submitform() {
var User_pass1 = document.getElementById("User_pass1").value.match("^\\S[0-9a-zA-Z.-]*$");
var User_pass2 = document.getElementById("User_pass2").value.match("^\\S[0-9a-zA-Z.-]*$");
var User_pass3 = document.getElementById("User_pass3").value.match("^\\S[0-9a-zA-Z.-]*$");
//document.getElementById("User_pass1").value="";
//document.getElementById("User_pass2").value="";
//document.getElementById("User_pass3").value="";
if (User_pass1 == null) {
alert("password field Required.....!");
return;
} else if (User_pass2 == null) {
alert(" New password field Required.....!");
return;
} else if (User_pass3 == null) {
alert("New password conformation field Required.....!");
return;
} else if (String(User_pass2) != String(User_pass3)) {
alert("New password and conformation not match....!");
return;
} else {
var str = "User_pass1=" + User_pass1 + "&User_pass2=" + User_pass2;
$.ajax({
type: "POST",
url: _baseurl + "/setting/",
data: str,
success: function (output)
{alert(output);}
});
return;
}
}