[英]Refresh form submission not working
After googling the web, I could not find the correct solution to my code. 上网搜索后,我找不到正确的代码解决方案。 Here I am linking the JQuery with ajax google libraries for HTML FORM validation.
在这里,我将JQuery与ajax google库链接以进行HTML FORM验证。 It works fine but not refreshing after form successful form submission.
成功提交表单后,它可以正常工作,但不会刷新。 Please guide me.
请指导我。
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
function Submit(){
var emailRegex = /^[A-Za-z0-9._]*\@[A-Za-z]*\.[A-Za-z]{2,5}$/;
var formemail = $("#femail").val();
var name = $("#name").val();
var name = $("#place").val();
var femail = $("#femail").val();
if($("#name").val() == "" ){
$("#name").focus();
$("#error").html("Enter the Name.");
return false;
}else if($("#place").val() == "" ){
$("#place").focus();
$("#error").html("Enter the Place.");
return false;
}else if($("#femail").val() == "" ){
$("#femail").focus();
$("#error").html("Enter the email.");
return false;
}else if(!emailRegex.test(formemail)){
$("#femail").focus();
$("#error").html("Enter the valid email.");
return false;
}else if($(name != '' && place != '' && femail != '')){
$("#error").html("Form submitted successfully.")
}
}
</script>
<form id="form_name" name="form" method="post" action=" ">
<div id="error"></div>
NAME: <input type="text" name="Name" id="name" > <br>
PLACE: <input type="text" name="Place" id="place"> <br>
EMAIL: <input type="text" name="Email" id="femail"> <br><br>
<input type="button" id="submit" value="Submit" onClick="Submit()"/>
</form>
Once the HTML form is submitted, it is showing "Form submitted successfully" then the form to be refreshed. 提交HTML表单后,将显示“表单提交成功”,然后刷新表单。
Having
type = 'button'
, it will not act assubmit-button
type = 'button'
,它将不充当submit-button
type = "submit"
type = "submit"
event.preventdefault()
to prevent form submission if client-side-validation fails event.preventdefault()
阻止表单提交 function Submit(e) { var emailRegex = /^[A-Za-z0-9._]*\\@[A-Za-z]*\\.[A-Za-z]{2,5}$/; var formemail = $("#femail").val(); var name = $("#name").val(); var name = $("#place").val(); var femail = $("#femail").val(); if ($("#name").val() == "") { $("#name").focus(); $("#error").html("Enter the Name."); e.preventDefault(); } else if ($("#place").val() == "") { $("#place").focus(); $("#error").html("Enter the Place."); e.preventDefault(); } else if ($("#femail").val() == "") { $("#femail").focus(); $("#error").html("Enter the email."); e.preventDefault(); } else if (!emailRegex.test(formemail)) { $("#femail").focus(); $("#error").html("Enter the valid email."); e.preventDefault(); } else if ($(name != '' && place != '' && femail != '')) { $("#error").html("Form submitted successfully.") } }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <form id="form_name" name="form" method="post" action=" "> <div id="error"></div> NAME: <input type="text" name="Name" id="name"> <br>PLACE: <input type="text" name="Place" id="place"> <br>EMAIL: <input type="text" name="Email" id="femail"> <br> <br> <input type="submit" id="submit" value="Submit" onClick="Submit(event)" /> </form>
Use below function... 使用以下功能...
location.reload(true);
window.location.reload()
window.location.href=window.location.href
In addition to the changes suggested by Rayon, you could refresh you form by placing $("#form_name")[0].reset()
right below $("#error").html("Form submitted successfully.")
: 除了Rayon建议的更改之外,您还可以通过将
$("#form_name")[0].reset()
放在$("#error").html("Form submitted successfully.")
下方来刷新表单:
function Submit(e){
e.preventDefault();
// You will have to submit your form using ajax.
var emailRegex = /^[A-Za-z0-9._]*\@[A-Za-z]*\.[A-Za-z]{2,5}$/;
var formemail = $("#femail").val();
var name = $("#name").val();
var name = $("#place").val();
var femail = $("#femail").val();
if($("#name").val() == "" ){
$("#name").focus();
$("#error").html("Enter the Name.");
return false;
}else if($("#place").val() == "" ){
$("#place").focus();
$("#error").html("Enter the Place.");
return false;
}else if($("#femail").val() == "" ){
$("#femail").focus();
$("#error").html("Enter the email.");
return false;
}else if(!emailRegex.test(formemail)){
$("#femail").focus();
$("#error").html("Enter the valid email.");
return false;
}else if($(name != '' && place != '' && femail != '')){
$("#error").html("Form submitted successfully.")
$("#form_name")[0].reset();
}
}
But if you are not submitting your form using ajax your page is going to get reloaded which defeats the purpose of displaying a "form submitted successfully" message. 但是,如果您不使用Ajax提交表单,则页面将被重新加载,这违背了显示“表单提交成功”消息的目的。
Two steps you should do in your program as follows 您应该在程序中执行以下两个步骤
< form id="form_name" name="form" method="post" action=" "> <form id =“ form_name” name =“ form” method =“ post” action =“”>
instead of above line u 而不是你上面的线
< form name="form" id="form_name" name="form" method="post" action=" " > <form name =“ form” id =“ form_name” name =“ form” method =“ post” action =“”>
2.add the line in your script "Submit" 2.在脚本“提交”中添加该行
document.form.reset(); document.form.reset();
//Here "form" is name of your form //这里“ form”是您的表单名称
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.