javascript 删除 html 表单的确认
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11638655/
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
Delete confirmation for html form
提问by user1548769
I would like to add a delete confirmation to my form button Possible javascript to use??
我想向我的表单按钮添加删除确认 可能使用 javascript??
<script type="text/javascript">
function confirmDelete() {
return confirm("Are you sure you want to delete?");
}
</script>
This is currently working inside my form.
这目前正在我的表单中工作。
<input type="button" onclick="parent.location='<?php echo "delete.php?id=" . $people['id'] ?>'" value='Delete'>
How can I integrate the javascript into the onclick event? Either using the above javascript or any other way that may be better
如何将 javascript 集成到 onclick 事件中?使用上面的javascript或任何其他可能更好的方式
采纳答案by jbabey
Bind your event handler in javascript.
在 javascript 中绑定您的事件处理程序。
<input type="button" id="delete" value='Delete' />
document.getElementById('delete').onclick = function () {
if (confirm('Are you sure?')) {
parent.location='<?php echo "delete.php?id=" . $people['id'] ?>';
}
};
回答by Jo?o Mosmann
It's possible :)
这是可能的 :)
On the tag, use the attribute onsubmit like this :
在标签上,像这样使用 onsubmit 属性:
<form onsubmit="return confirm('Are you sure?');">
or using a method
或使用方法
<form onsubmit="return myMethod">
The same for a button on onclick event. If it return false, it will be not clicked.
onclick 事件上的按钮也是如此。如果返回false,则不会被点击。
Remember, if the method return false, the form will not be submited
请记住,如果该方法返回false,则不会提交表单
回答by user3788986
I hppe this will help you
我hppe这会帮助你
It work for me
它对我有用
<a href="index.php?action=del&id=125" onclick="return confirm('Are you sure you want to delete?')">Delete</a>
回答by HatSoft
How can I integrate the javascript into the onclick event?
如何将 javascript 集成到 onclick 事件中?
<input type="button" onclick="javascript:confirmDelete();" value='Delete'>
and
和
<script type="text/javascript">
function confirmDelete() {
var status = confirm("Are you sure you want to delete?");
if(status)
{
parent.location.replace("parent.location='<?php echo "delete.php?id=" . $people['id'] ?>'");
}
}
</script>
回答by Saddam Khan
First of all add an onclick method in your HTML, like:
首先在 HTML 中添加一个 onclick 方法,例如:
**HTML:**
<a class="btn btn-danger" href="some url" onclick="DeleteItem();"></a>
**JavaScript:**
function DeleteItem()
{
var a = confirm("Are you sure to delete this record?");
if(a) {
return true;
}else{
return false;
}
}
回答by dykeag
HTML also supports a reset button on the form, if you are not attached to using javaScript. You can have a structure like this:
HTML 还支持表单上的重置按钮,如果您不附加使用 javaScript。你可以有这样的结构:
<form>
...
items in your form
...
<input type="reset">
...
</form>
When the reset button is clicked, all of the form inputs will reset to their default values.
单击重置按钮后,所有表单输入都将重置为其默认值。