最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Delete confirmation for html form - Stack Overflow

programmeradmin4浏览0评论

I would like to add a delete confirmation to my form button Possible javascript to use??

<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

I would like to add a delete confirmation to my form button Possible javascript to use??

<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

Share Improve this question edited Jul 24, 2012 at 20:19 Pshemo 124k25 gold badges191 silver badges275 bronze badges asked Jul 24, 2012 at 20:16 user1548769user1548769 3193 gold badges5 silver badges12 bronze badges 1
  • similar: stackoverflow.com/questions/9139075/… – Ciro Santilli OurBigBook.com Commented May 10, 2016 at 9:21
Add a comment  | 

7 Answers 7

Reset to default 9

It's possible :)

On the tag, use the attribute onsubmit like this :

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

Remember, if the method return false, the form will not be submited

How can I integrate the javascript into the onclick event?

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

I hppe this will help you

It work for me

<a href="index.php?action=del&id=125" onclick="return confirm('Are you sure you want to delete?')">Delete</a>

First of all add an onclick method in your HTML, like:

**HTML:**
<a class="btn btn-danger" href="some url" onclick="DeleteItem();"></a>
**JavaScript:**
function DeleteItem() 
{
     return confirm("Are you sure to delete this")
}
// Or you can simply do it like below:
<button onclick="javascript: return confirm('Are you sure to delete?');">Delete</button>

Bind your event handler in 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'] ?>';
    }
};

Add button element with onclick event handler as below:

<input type="submit" name="delete" value="Delete" onclick="return confirmDelete();">

and the JavaScript content as below:

function confirmDelete() { 
    return confirm("Are you sure you want to delete?");   
} 

It works for me.

HTML also supports a reset button on the form, if you are not attached to using javaScript. You can have a structure like this:

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

发布评论

评论列表(0)

  1. 暂无评论