简体   繁体   English

JavaScript 表单提交 - 确认或取消提交对话框

[英]JavaScript Form Submit - Confirm or Cancel Submission Dialog Box

For a simple form with an alert that asks if fields were filled out correctly, I need a function that does this:对于一个带有询问是否正确填写字段的警报的简单表单,我需要一个 function 来执行此操作:

  • Shows an alert box when button is clicked with two options:单击按钮时显示一个警告框,有两个选项:

    • If "OK" is clicked, the form is submitted如果单击“确定”,则提交表单
    • If cancel is clicked, the alert box closes and the form can be adjusted and resubmitted如果单击取消,则警告框关闭,并且可以调整并重新提交表单

I think a JavaScript confirm would work but I can't seem to figure out how.我认为 JavaScript 确认会起作用,但我似乎无法弄清楚如何。

The code I have now is:我现在的代码是:

 function show_alert() { alert("xxxxxx"); }
 <form> <input type="image" src="xxx" border="0" name="submit" onclick="show_alert();" alt="PayPal - The safer, easier way to pay online!" value="Submit"> </form>

A simple inline JavaScript confirm would suffice:一个简单的内联 JavaScript 确认就足够了:

<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:除非您正在进行验证,否则不需要外部 function ,您可以执行以下操作:

<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);">

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();
}

You could use the JS confirm function.您可以使用 JS 确认 function。

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

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

Simple and easy :简单易行

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

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>

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始终牢记一件事,方法操作属性在onsubmit属性之后写入

javascript code javascript码

function checkEmpData()
{
  var a = 0;

  if(a != 0)
  {
    return confirm("Do you want to generate attendance?");
  }
   else
   {
      alert('Please Select Employee First');
      return false;
   }  
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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