简体   繁体   中英

Javascript Popup Confirm

I have a form:

 <form action="addaddresspage3.html" method="post" id="form" class="form">

with the following submit:

 <input type="submit"  id="continue" value="Add Address"/>

So the user clicks on the button, they are then directed to addaddresspage3.html. The form works completely fine. But I was wondering is there way to have some sort of confirmation option (either JavaScript or jQuery I'm guessing). For example the user clicks the button, then a message (can be a pop-up?) saying something like "Are you sure you wish to continue?", then the user clicks yes and they are directed to addaddresspage3.html or then click no and they stay on the same page. I have seen various JS popups, but they just show messages and I can't seem to edit them to make the user direct to a page when they click ok

Sure, you can just add an onclick attribute to your button that uses a Javascript confirm() call and returns the user selection (submitting the form if they choose yes and staying on the same page if they choose no) :

<input type="submit" ... onclick='return confirm("Are your sure you want to continue?");'/>

You can see the most basic demonstration of this here and seen below :

<form>
    <input type="submit" onclick='return confirm("Are your sure you want to continue?");'/>
</form>

You can use jquery event beforeunload to ask user if he want to leave the current page. Use it like below:

$(window).bind('beforeunload', function(){
  return 'Are you sure you want to leave?';
});
<form action="addaddresspage3.html" method="post" id="form" class="form" onsubmit="javascript:return getConfirm();">
    <input type="submit" id="continue" value="Add Address"/>
</form>
<script>
function getConfirm(){
    if(!window.confirm('Are you sure?!'))
        return false;
    return true;
}
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM