admin管理员组

文章数量:1289496

I have a simple form with one hidden input field, and a submit button. When the user clicks the submit button, I want it first to run a JavaScript function with a confirm() function. Depending on whether the user hits 'Ok' or 'Cancel', I want the form to submit, or to not submit. How do I successfully assign an on-click function to a submit button, and have it work as described above?

function confirmDelete() {
var del = confirm("Are you sure you wish to delete this post?");

if (del)
  //continue and submit the form
else
  //the form should not be submitted
}

Here is the form:

<form action="/delete/" method="post">
<input type="hidden" name="path" value="'.$path.'" />
</form>

I realize that I could scrap the form altogether and just pass the hidden variable as a GET variable to the /delete/ page, however I would prefer to use a POST variable for security reasons.

I have a simple form with one hidden input field, and a submit button. When the user clicks the submit button, I want it first to run a JavaScript function with a confirm() function. Depending on whether the user hits 'Ok' or 'Cancel', I want the form to submit, or to not submit. How do I successfully assign an on-click function to a submit button, and have it work as described above?

function confirmDelete() {
var del = confirm("Are you sure you wish to delete this post?");

if (del)
  //continue and submit the form
else
  //the form should not be submitted
}

Here is the form:

<form action="/delete/" method="post">
<input type="hidden" name="path" value="'.$path.'" />
</form>

I realize that I could scrap the form altogether and just pass the hidden variable as a GET variable to the /delete/ page, however I would prefer to use a POST variable for security reasons.

Share Improve this question edited Aug 22, 2012 at 3:09 Claudio Redi 68.4k15 gold badges137 silver badges160 bronze badges asked Aug 22, 2012 at 3:03 fvgsfvgs 22k9 gold badges36 silver badges49 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

This would suffice:

<input type="submit" value="Delete" onClick="return confirm('Are you sure?');" />

本文标签: