HTML - 如何对提交按钮执行确认弹出窗口,然后发送请求?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16849117/
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
HTML - How to do a Confirmation popup to a Submit button and then send the request?
提问by Mona
I am learning web development using Django
and have some problems in where to put the code taking chage of whether to submit the request in the HTML code
.
我正在学习使用 Web 开发,Django
并且在将代码放在哪里以决定是否在HTML code
.
Eg. There is webpage containing a form
(a blog) to be filled by the user, and upon click on the Save button,there is a pop up asking whether you want to confirm
or not. If click on confirm
, then the request is sent.
例如。有一个网页包含一个form
(博客)供用户填写,点击保存按钮后,会弹出一个询问您是否要填写的信息confirm
。如果单击confirm
,则发送请求。
I searched and find this javascript
code.
我搜索并找到了这个javascript
代码。
<script type="text/javascript">
function clicked() {
alert('clicked');
}
<input type="submit" onclick="clicked();" value="Button" />
But I guess this is not the correct function as it seems to me that whenever you click on the Button, the request will be submitted. So How can I delay the submit request until user has confirm the submit?
但我想这不是正确的功能,因为在我看来,每当您单击按钮时,都会提交请求。那么如何延迟提交请求,直到用户确认提交?
采纳答案by DaneoShiga
I believe you want to use confirm()
我相信你想使用confirm()
<script type="text/javascript">
function clicked() {
if (confirm('Do you want to submit?')) {
yourformelement.submit();
} else {
return false;
}
}
</script>
回答by Isaac
The most compact version:
最紧凑的版本:
<input type="submit" onclick="return confirm('Are you sure?')" />
The key thing to note is the return
需要注意的关键是 return
-
——
Because there are many ways to skin a cat, here is another alternate method:
因为有很多方法可以给猫剥皮,这里是另一种替代方法:
HTML:
HTML:
<input type="submit" onclick="clicked(event)" />
Javascript:
Javascript:
<script>
function clicked(e)
{
if(!confirm('Are you sure?'))e.preventDefault();
}
</script>
回答by Matt Ball
Use window.confirm()
instead of window.alert()
.
使用window.confirm()
代替window.alert()
。
HTML:
HTML:
<input type="submit" onclick="return clicked();" value="Button" />
JavaScript:
JavaScript:
function clicked() {
return confirm('clicked');
}
回答by Manjunath
<script type='text/javascript'>
function foo() {
var user_choice = window.confirm('Would you like to continue?');
if(user_choice==true) {
window.location='your url'; // you can also use element.submit() if your input type='submit'
} else {
return false;
}
}
</script>
<input type="button" onClick="foo()" value="save">
回答by Tiego Araujo
Another option that you can use is:
您可以使用的另一个选项是:
onclick="if(confirm('Do you have sure ?')){}else{return false;};"
onclick="if(confirm('你确定吗?')){}else{return false;};"
using this function on submit button you will get what you expect.
在提交按钮上使用此功能,您将得到您所期望的。