javascript .jsp 中的确认删除
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8299391/
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
Confirmation delete in .jsp
提问by OakvilleWork
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 事件:
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?
如何将其合并到我的 onClick 事件中,使其成为一行,不想使用更多 javascript?如果我必须这样做,那么我将如何在 javascript 函数的消息框中合并“是”的操作?
回答by BalusC
If I understand you correctly, you just want
如果我理解正确,你只是想要
<form action="/Config?pg=FIBiller&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.
或者,如果由于某些不清楚的原因(可能是设计不好?),您确实无法更改表单,那么您需要引入一个if-else
块,如下所示,它更改表单的确认操作和false
取消返回。
<input type="submit" value="delete" onclick="if (confirm('Are you sure you want to delete?')) form.action='/Config?pg=FIBiller&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&cmd=delete'; } else { return false; }" />
Unrelatedto 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:
与具体问题无关,我建议摆脱,cmd=delete
因为您也可以通过为删除按钮命名来检查它,以便它也将作为请求参数发送:
<input type="submit" name="delete" value="delete" ...>
you could then check as follows in the JSP/Servlet side if it's been pressed:
如果按下了,您可以在 JSP/Servlet 端进行如下检查:
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 comes 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?
顺便说一句,通过 GET 删除是一个坏主意。而是使用POST。否则,当搜索机器人出现在您的网站并抓取所有 GET 链接/操作而不执行 JavaScript 时,将执行所有删除链接。您也不希望生成的删除请求可以添加书签,对吗?