简体   繁体   English

jQuery链接确认

[英]Jquery link confirmation

I am implementing a form which has got links in it. 我正在实现一个包含链接的表单。 like 喜欢

<form>
<a>FAQ </a> /* something this way */
<submit button>
</form>

I have to display a confirmation box to the user, if the link is clicked and only when it tries to load the href.In case the user is opening the link in new tab or uses 'CMD-Click'(Mac), the prompt must not be shown. 如果单击链接并且仅当它尝试加载href时,我必须向用户显示一个确认框。如果用户在新选项卡中打开链接或使用'CMD-Click'(Mac),则提示不得显示。 In Firefox, the browser itself takes care of this when the user tries to navigate to another page when he is in the middle of the form. 在Firefox中,当用户在表单中间时尝试导航到另一个页面时,浏览器会自己处理。 But I need this functionality to work in all browsers. 但是我需要此功能才能在所有浏览器中使用。

Does anyone know how to do this? 有谁知道如何做到这一点?

Thanks in Advance. 提前致谢。

that's simple 很简单

<a href="http://www.google.com" onclick="return confirm('are you sure you want to go to Google?');">Google</a>

but i'm not sure if CMD+Click doesn't alert the user. 但我不确定CMD + Click是否不会提醒用户。 Most of these event can't be controlled by javascript as they are coded into browsers. 这些事件大多数都无法通过JavaScript进行控制,因为它们已被编码到浏览器中。

Maybe something like this? 也许是这样的吗? demo 演示

  <form>
        <a href="http://www.google.com">FAQ </a> /* something this way */
         <submit button>
  </form>




    $("a").click(function() {
        if (!confirm("Do you want to leave?")) {
            return false;
        }

    });

As for not displaying on new tab/new window, there are no such javascript events, thus you cannot capture them. 至于不显示在新选项卡/新窗口上,则没有此类javascript事件,因此您无法捕获它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM