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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6515502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 22:07:41  来源:igfitidea点击:

JavaScript Form Submit - Confirm or Cancel Submission Dialog Box

javascripthtmlformssubmitconfirm

提问by matt

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 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>

回答by Samuel Liew

A simple inline JavaScript confirmwould suffice:

一个简单的内联 JavaScript 确认就足够了:

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

No need for an external functionunless 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);">

回答by Majid Fouladpour

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

回答by Jason Gennaro

You could use the JS confirm function.

您可以使用 JS 确认功能。

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

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

http://jsfiddle.net/jasonennaro/DBHEz/

回答by Pablo Salazar

Simple and easy:

简单易行

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

回答by Alireza

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>

回答by Sumit Kumar Gupta

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 methodand actionattribute write after onsubmitattributes

永远记住一件事,methodaction属性写在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;
   }  
}