Javascript确认对话框 [英] Javascript confirm dialog

查看:86
本文介绍了Javascript确认对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向删除按钮添加一个确认对话框,询问用户是否正确删除所选项目。

I want to add a confirm dialog to a delete button to ask the user whether it is ok or not deleting the selected item.

如果不是,则不应该发生任何事情,否则应该执行一个url。

If not, nothing should happend, else a url should be executed.

我知道如何通过一些javascript代码来实现这一点,但我正在寻找一种代码少的解决方案。我的意思是:

I know how to realize this via some javascript code but I am looking for a solution that has less code. I mean e.g. :

<a href="mysite.de/xy/delete" onClick="...">Delete</a>

是否有可能将整个功能放在onClick元素中而不在头部添加额外的javascript?

Is it possible to put the whole functionality in the onClick element without having some extra javascript in the header?

推荐答案

更好(虽然远非理想!):转过来。

Better (though far from ideal!): turn it around. Don't let the link do anything, unless you got JavaScript:

<a href="#" 
  onclick="if confirm('Sure?') { window.location='http://mysite.de/xy/delete';}">
    Click to delete
</a>

这至少可以防止链接在没有JavaScript的情况下工作。这也降低了Google无意抓取链接的风险,甚至可以降低一些本地插件的风险。 (图片如果你有一个插件会尝试加载/显示为缩略图)悬停链接上的目标页面!)

This at least prevents the link to work without JavaScript. This also reduces the risk of the link accidentally being crawled by Google, or even by some local plugin. (Image if you had a plugin that would try to load/show as thumbnail) the target page on hover of a link!)

但是,这个解决方案并不理想。您将实际浏览到该网址,因此可能会在历史记录中显示该网址。您实际上可以删除Bob,创建一个新的Bob,然后通过点击浏览器中的'back'来意外删除它!

Still, this solution is not ideal. You will actually browse to the url, and the url might show up in the history because of that. You could actually delete Bob, create a new Bob, and then delete that one by accident by just clicking 'back' in the browser!

更好的选择是使用JavaScript或表单发布所需的操作。您可以使用POST方法向服务器发出请求,或者说可以更好地使用DELETE方法。这也应该阻止这些网址被索引。

A better option would be to use JavaScript or a form to post the desired action. You can make a request to the server with the POST method, or arguably better, the DELETE method. That should also prevent the urls from being indexed.

这篇关于Javascript确认对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆