Javascript 表单提交前确认
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6493009/
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
Confirm before a form submit
提问by sark9012
I have searched for an answer but couldn't find one!
我一直在寻找答案,但找不到!
I have a simple form,
我有一个简单的表格,
<form action="adminprocess.php" method="POST">
<input type="submit" name="completeYes" value="Complete Transaction" />
</form>
How would I adjust this to confirm before processing the form?
在处理表单之前,我将如何调整以确认?
I tried onclick, but couldn't get it working.
我试过onclick,但无法让它工作。
Any ideas?
有任何想法吗?
UPDATE - What I now have.
更新 - 我现在拥有的。
<script type="text/javascript">
var el = document.getElementById('myCoolForm');
el.addEventListener('submit', function(){
return confirm('Are you sure you want to submit this form?');
}, false);
</script>
<form action="adminprocess.php" method="POST" id="myCoolForm">
<input type="submit" name="completeYes" value="Complete Transaction" />
</form>
回答by Michael J.V.
HTML:
HTML:
<form action="adminprocess.php" method="POST" id="myCoolForm">
<input type="submit" name="completeYes" value="Complete Transaction" />
</form>
JavaScript:
JavaScript:
var el = document.getElementById('myCoolForm');
el.addEventListener('submit', function(){
return confirm('Are you sure you want to submit this form?');
}, false);
Edit: you can always use inline JS code like this:
编辑:你总是可以像这样使用内联 JS 代码:
<form action="adminprocess.php" method="POST" onsubmit="return confirm('Are you sure you want to submit this form?');">
<input type="submit" name="completeYes" value="Complete Transaction" />
</form>
回答by Greenisha
<input type="submit" onclick="return confirm('Are you sure you want to do that?');">
回答by Maxim Krizhanovsky
The correct event is onSubmit() and it should be attached to the form. Although I think it's possible to use onClick, but onSubmit is the correct one.
正确的事件是 onSubmit() 并且它应该附加到表单中。虽然我认为可以使用 onClick,但 onSubmit 是正确的。
回答by Atticus
If you're already using jQuery.. you can use an event handler to trigger before submission
如果您已经在使用 jQuery .. 您可以使用事件处理程序在提交之前触发
$(document).ready(function() {
$("#formID").submit(function(){
// handle submission
});
});
Reference: http://api.jquery.com/submit/
回答by antelove
var submit = document.querySelector("input[type=submit]");
/* set onclick on submit input */
submit.setAttribute("onclick", "return test()");
//submit.addEventListener("click", test);
function test() {
if (confirm('Are you sure you want to submit this form?')) {
return true;
} else {
return false;
}
}
<form action="admin.php" method="POST">
<input type="submit" value="Submit" />
</form>
回答by Rami Alloush
In my case, I didn't have a form ID and couldn't add inline in the form
tag. I ended up with the following jQuery code
就我而言,我没有表单 ID,也无法在form
标签中添加内联。我最终得到了以下 jQuery 代码
var form = $("form").first();
form.on('submit', function() {
return confirm('Are you sure you want to submit this form?');
});
回答by Dextr
if you have more then one submit buttons that do different actions you can do it this way.
如果您有多个执行不同操作的提交按钮,您可以这样做。
<input TYPE=SUBMIT NAME="submitDelete" VALUE="Delete Script" onclick='return window.confirm("Are you sure you want to delete this?");'>