Javascript HTML - 表单提交按钮确认对话框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3632530/
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 05:30:51  来源:igfitidea点击:

HTML - Form Submit Button Confirmation Dialog

phpjavascriptform-submit

提问by Moon

this is a general form code

这是一个通用表格代码

<form name="search_form" action="" method="POST">
<input type="text" name="search_text">          
<input type="submit" name="search_bt" value="Go">
</form>

is there a way could have a confirmation dialog saying 'Yes'\'No' or 'Confirm'\'Cancel' etc...

有没有办法可以有一个确认对话框说“是”\“否”或“确认”\“取消”等...

One way i figured of dong is is with CSS Layer and JavaScript and Php... that have a php isset(){}chechk on the button and when set display a Div displayed with two buttons and onclick=func()JS function of those buttons have a php variable(flag) set and then i can if(flag){}to continue or skip some code...

我认为 dong 的一种方法是使用 CSS 层和 JavaScript 和 PHP ...isset(){}在按钮上有一个 php chechk,当设置显示一个 Div 时显示两个按钮onclick=func(),这些按钮的 JS 函数设置了一个 php 变量(标志)然后我可以if(flag){}继续或跳过一些代码...

well that is going to work and plus point is that i can have a well themed dialog box but i just wanna make my life easier...

好吧,这将起作用,另外一点是我可以有一个主题良好的对话框,但我只是想让我的生活更轻松......

回答by acqu13sce

You can also do it with one line in the form tag itself

您也可以使用表单标签本身中的一行来完成

<form action="exampleHandlerPage.php" method="post" onsubmit="return confirm('Are you sure you want to submit?');">

回答by matepal297

If you have 2 or more submit buttons in one form:

如果您在一种形式中有 2 个或更多提交按钮:

<input type="submit" value="Edit">
<input type="submit" name="delete" value="Delete" onclick="return confirm('Confirm, please.');">

The dialog shows up only when you click the Deletebutton.

该对话框仅在您单击Delete按钮时显示。

回答by Garis M Suero

Using raw javascriptwithout any div...

javascript没有任何 div 的情况下使用 raw ...

You can have this function

你可以有这个 function

function confirmSubmit() {
  if (confirm("Are you sure you want to submit the form?")) {
    document.getElementById("FORM_ID").submit();
  }
  return false;
}

And you can call that function from the onsubmitevent in the form, or on the onclickevent in the button.

您可以从onsubmit表单中的事件中调用该函数,也可以onclickbutton.

By the way, have you heard about JQuery. Its a JS Library with a lot of helpful things that give you a comfort and beauty way of coding javascript.

顺便说一下,你听说过JQuery. 它是一个 JS 库,里面有很多有用的东西,给你一种舒适和美观的 javascript 编码方式。

As an example of what you want to get done, take this confirmation dialogfrom JQueryas example

至于你想要什么例子来得到完成,借此确认对话框来自 JQuery为例

回答by Surya Tanamas

<form action="<form handler>" method="post" onsubmit="return confirm('Are you sure you want to submit?')">

is this javascript can be store diferent from tag "form", replace

这个javascript可以存储与标签“表单”不同的地方,替换

return confirm(...)

with something like

return sendata(...)