javascript 使用 window.location 和 return 一起确认

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5021110/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 15:41:37  来源:igfitidea点击:

Using window.location and return confirm together

javascript

提问by Cameron

I have this button that will send a user to a page but first ask the user to confirm, but it's not working correctly, how can I fix it? Thanks

我有这个按钮可以将用户发送到一个页面,但首先要求用户确认,但它无法正常工作,我该如何解决?谢谢

<input class="cancel" type="button" value="Cancel" onclick="window.location.href='<?php bloginfo('home'); ?>';return confirm('Are you sure you want to delete your post?');" />

<input class="cancel" type="button" value="Cancel" onclick="window.location.href='<?php bloginfo('home'); ?>';return confirm('Are you sure you want to delete your post?');" />

回答by simshaun

Try this:

试试这个:

<input class="cancel" type="button" value="Cancel" onclick="if (confirm('Are you sure you want to delete your post?')) window.location.href='http://www.google.com';" />

回答by Daniel A. White

Write a new function

编写一个新函数

void redirect(url) {
  if (confirm('Are you sure you want to delete your post?')) {
    window.location.href=url;
  }
  return false;
}

then from your onclick

然后从你的 onclick

onclick="redirect('<?php bloginfo('home'); ?>');"

回答by Pekka

Turn the commands around. You are leaving the current page, thendoing the confirm. That can't work.

扭转命令。您正在离开当前页面,然后进行确认。那行不通。

if (confirm(...))  window.location.href='....'