简体   繁体   English

单击链接时使用JavaScript显示确认弹出窗口

[英]Display confirmation popup with JavaScript upon clicking on a link

How do I make one of those hyperlinks where when you click it, it will display a popup asking "are you sure?" 如何制作其中一个超链接,当您点击它时,它会显示一个弹出窗口,询问“你确定吗?”

<INPUT TYPE="Button" NAME="confirm" VALUE="???" onClick="message()">

I already have a message() function working. 我已经有一个message()函数正在运行。 I just need to know what the input type for a hyperlink would be. 我只需要知道超链接的输入类型是什么。

<a href="http://somewhere_else" onclick="return confirm()">

When the user clicks the link, the confirm function will be called. 当用户单击链接时,将调用confirm函数。 If the confirm function returns false , the link traversal is cancelled, if true is returned, the link is traversed. 如果confirm函数返回false ,则取消链接遍历,如果返回false ,则遍历链接。

<a href="http://something.com" onclick="return confirmAction()">try to click, I dare you</a>

with the function 与功能

function confirmAction(){
      var confirmed = confirm("Are you sure? This will remove this entry forever.");
      return confirmed;
}

(you can also return the confirm right away, I separated it for the sake of readability) (您也可以立即返回确认,我为了便于阅读而将其分开)

Tested in FF, Chrome and IE 经过FF,Chrome和IE测试

As Nahom said, except I would put the javascript:message() call directly in the href part (no need for onclik then). 正如Nahom所说,除了我将javascript:message()直接调用到href部分(不需要onclik然后)。

Note: leaving the JavaScript call in the onClick has a benefit: in the href attribute, you can put a URL to go to if the user doesn't have JavaScript enabled. 注意:在onClick中保留JavaScript调用有一个好处:在href属性中,如果用户未启用JavaScript,则可以放置URL。 That way, if they do have JS, your code gets run. 这样,如果他们有JS,你的代码就会运行。 If they don't, they go somewhere where they are instructed to enable it (perhaps). 如果他们不这样做,他们会去指示他们启用它的地方(也许)。

Now, your message routine must not only ask the question, but also use the answer: if positive, it must call submit() on the form to post the form. 现在,您的message例程不仅要问问题,还要使用答案:如果是肯定的,则必须在表单上调用submit()来发布表单。 You can pass this in the call to ease the fetching of the form. 您可以在调用中传递this以简化表单的提取。

Personally, I would go for a button (input tag as you show) instead of a simple link to do the process: it would use a more familiar paradigm for the users. 就个人而言,我会选择一个按钮(显示输入标签)而不是一个简单的链接来执行该过程:它将为用户使用更熟悉的范例。

[EDIT] Since I prefer to verify answers I give, I wrote a simple test: [编辑]由于我更愿意验证我给出的答案,我写了一个简单的测试:

<script type="text/javascript" language="JavaScript">
function AskAndSubmit(t)
{
  var answer = confirm("Are you sure you want to do this?");
  if (answer)
  {
    t.form.submit();
  }
}
</script>

<form action="Tests/Test.html" method="GET" name="subscriberAddForm">
<input type="hidden" name="locationId" value="2721"/>
<input type="text" name="text" value="3.1415926535897732384"/>
<input type="button" name="Confirm" value="Submit this form" onclick="AskAndSubmit(this)"/>
</form>

Yes, the submit just reload the page here... Tested only in FF3. 是的,提交只是在这里重新加载页面...仅在FF3中测试过。

[EDIT] Followed suggestion in the comments... :-) [编辑]评论中的后续建议...... :-)

<a href="#" onclick="message(); return false;">???</a>

只有当点击不需要将用户导航到另一个页面时,此答案才可以。

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

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