单击提交按钮后如何保持在同一页面上 [英] how to stay on the same page after clicking submit button
问题描述
我正在创建一个登录表单,如果任何输入错误,那么我想停留在同一页面上,并通知客户端输入正确的输入 我想留在此页面 http://localhost:8080/loginpage/,但是每次单击后我m重定向到
I'm creating a login form and in that if any input is wrong then i want to stay on the same page and notify the client to enter the correct input I want to stay on this page http://localhost:8080/loginpage/ but after every click i'm redirected to http://localhost:8080/loginpage/?UserName=UserName&pword=&ConfirmPassword=&Email=Email&FirstName=First+Name&LastName=Last+Name&cars=male&Signup=Signup .
我已经为此编写了一个代码,但是它似乎不起作用.
I have written a code for this but it does not seem to work.
if(t!==0)
{
var er="Email-id already exists";
window.location.reload(false);
document.getElementById("nemail").value=er;
document.getElementById("username").value=username;
document.getElementById("pword").value="";
document.getElementById("confpwd").value="";
document.getElementById("fname").value=fname;
document.getElementById("lname").value=lname;
document.getElementById("gender").value=gender;
}
我尝试使用其他几种方法,例如
I have tried to use several other methods like
window.location.replace('/loginpage/');
window.location.href="/loginpage/index.html";
但是它们都不起作用. 请帮忙!
But none of them works. Please help!
推荐答案
有两种方法可以阻止表单提交.
There are two ways to prevent a form to submit.
- 设置表格
onsubmit="return false"
. - 在from上注册一个提交事件.在回叫中
event.preventDefault()
;
- Set form
onsubmit="return false"
. - Register a submit event on a from. In the callback call
event.preventDefault()
;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h2>1 way to prevent form to submit via onclick attribute</h2>
<form action="#submit-from-1-way" onsubmit="return validate(this)"><!--set onsubmit="return false"-->
<input name="value"/>
<button type="submit">Submit</button>
</form>
<h2>2 way to prevent form to submit via submit event</h2>
<form id="form" action="#submit-from-2-way"><!--set onsubmit="return false"-->
<input name="value"/>
<button type="submit">Submit</button>
</form>
<script>
console.log(location.href+'#'+location.hash+'?'+location.search);
function validate(form) {
return $(form).find('input').val();
}
$('#form').submit(function (e) {
if (!validate(this)) e.preventDefault();
});
</script>
这篇关于单击提交按钮后如何保持在同一页面上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!