最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to stay on the same page after clicking submit button - Stack Overflow

programmeradmin2浏览0评论

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!

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!

Share Improve this question asked Mar 1, 2017 at 21:22 aeshnaaeshna 831 gold badge2 silver badges9 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 1

There are two ways to prevent a form to submit.

  1. Set form onsubmit="return false".
  2. Register a submit event on a from. In the callback call event.preventDefault();

<script src="https://ajax.googleapis./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>

You can use preventDefault() on the click event for a given html object. This will prevent the browser from taking the default action on a specific HTML object.

For example to prevent actoin on a link:

<body>
 <a href="http://www.google." id="clickMe">Click Me</a>
<script>

function dontGo(event) {
    event.preventDefault();
}

document.getElementById("clickMe").addEventListener("click",dontGo);

</script>

</body>
发布评论

评论列表(0)

  1. 暂无评论