admin管理员组

文章数量:1302351

I wish to have a confirmation on a delete button, and am attempting to do it all in one line... this is the regular onClick event without the delete confimration:

onClick='this.form.action="/Config?pg=FIBiller&cmd=delete";'    

I found online that a confirm delete can be implemented as such:

"return confirm('Are you sure you want to delete?')"

How can I incorporate this into my onClick event so it's one line, don't want to use more javascript? If I must do so, than how would I incorporate the action for a "yes" on the message box in the javascript function?

I wish to have a confirmation on a delete button, and am attempting to do it all in one line... this is the regular onClick event without the delete confimration:

onClick='this.form.action="/Config?pg=FIBiller&cmd=delete";'    

I found online that a confirm delete can be implemented as such:

"return confirm('Are you sure you want to delete?')"

How can I incorporate this into my onClick event so it's one line, don't want to use more javascript? If I must do so, than how would I incorporate the action for a "yes" on the message box in the javascript function?

Share edited Aug 18, 2015 at 10:19 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Nov 28, 2011 at 16:59 OakvilleWorkOakvilleWork 2,3775 gold badges25 silver badges37 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

If I understand you correctly, you just want

<form action="/Config?pg=FIBiller&amp;cmd=delete">
    <input type="submit" value="delete" onclick="return confirm('Are you sure you want to delete?')" />
</form>

Or if you really can't change your form as such for some unclear reason (bad design maybe?), then you need to introduce an if-else block as follows which changes the form's action on confirmation and returns false on cancel.

<input type="submit" value="delete" onclick="if (confirm('Are you sure you want to delete?')) form.action='/Config?pg=FIBiller&amp;cmd=delete'; else return false;" />

Note: if you want to add more calls or want to improve readability, introduce braces:

<input type="submit" value="delete" onclick="if (confirm('Are you sure you want to delete?')) { form.action='/Config?pg=FIBiller&amp;cmd=delete'; } else { return false; }" />

Unrelated to the concrete problem, I'd suggest to get rid of cmd=delete as you could also just check that by giving the delete button a name so that it will be sent as request parameter as well:

<input type="submit" name="delete" value="delete" ...>

you could then check as follows in the JSP/Servlet side if it's been pressed:

if (request.getParameter("delete") != null) {
    // Delete button is pressed.
}

By the way, deleting by GET is a bad idea. Rather use POST. Otherwise all delete links will be executed when a searchbot es along your website and crawls all GET links/actions without executing JavaScript. You also don't want the resulting delete requests to be bookmarkable, right?

本文标签: javascriptConfirmation delete in jspStack Overflow