JavaScript Form Submit - Confirm or Cancel Submission Dialog Box

前端 未结 6 1425
一生所求
一生所求 2020-11-22 15:13

For a simple form with an alert that asks if fields were filled out correctly, I need a function that does this:

  • Shows an alert box when button is clicked w

相关标签:
6条回答
  • 2020-11-22 15:31

    You could use the JS confirm function.

    <form onSubmit="if(!confirm('Is the form filled out correctly?')){return false;}">
      <input type="submit" />
    </form>
    

    http://jsfiddle.net/jasongennaro/DBHEz/

    0 讨论(0)
  • 2020-11-22 15:34

    The issue pointed in the comment is valid, so here is a different revision that's immune to that:

    function show_alert() {
      if(!confirm("Do you really want to do this?")) {
        return false;
      }
      this.form.submit();
    }
    
    0 讨论(0)
  • 2020-11-22 15:36

    If you want to apply some condition on form submit then you can use this method

    <form onsubmit="return checkEmpData();" method="post" action="process.html">
      <input type="text" border="0" name="submit" />
      <button value="submit">submit</button>
    </form>
    

    One thing always keep in mind that method and action attribute write after onsubmit attributes

    javascript code

    function checkEmpData()
    {
      var a = 0;
    
      if(a != 0)
      {
        return confirm("Do you want to generate attendance?");
      }
       else
       {
          alert('Please Select Employee First');
          return false;
       }  
    }
    
    0 讨论(0)
  • 2020-11-22 15:41

    OK, just change your code to something like this:

    <script>
    function submit() {
       return confirm('Do you really want to submit the form?');
    }
    </script>
    
    <form onsubmit="return submit(this);">
       <input type="image" src="xxx" border="0" name="submit" onclick="show_alert();"
          alt="PayPal - The safer, easier way to pay online!" value="Submit">
    </form>
    

    Also this is the code in run, just I make it easier to see how it works, just run the code below to see the result:

    function submitForm() {
      return confirm('Do you really want to submit the form?');
    }
    <form onsubmit="return submitForm(this);">
      <input type="text" border="0" name="submit" />
      <button value="submit">submit</button>
    </form>

    0 讨论(0)
  • 2020-11-22 15:46

    A simple inline JavaScript confirm would suffice:

    <form onsubmit="return confirm('Do you really want to submit the form?');">
    

    No need for an external function unless you are doing validation, which you can do something like this:

    <script>
    function validate(form) {
    
        // validation code here ...
    
    
        if(!valid) {
            alert('Please correct the errors in the form!');
            return false;
        }
        else {
            return confirm('Do you really want to submit the form?');
        }
    }
    </script>
    <form onsubmit="return validate(this);">
    
    0 讨论(0)
  • 2020-11-22 15:55

    Simple and easy :

    <form onSubmit="return confirm('Do you want to submit?') ">
      <input type="submit" />
    </form>

    0 讨论(0)
提交回复
热议问题