javascript 重定向确认为真

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

Redirecting on confirm true

javascriptjqueryconfirm

提问by Dylan Buth

I want to make it so the user clicks a link, they get a confirm box. If they click OK, they get sent to a new php function.

我想让用户点击一个链接,他们会得到一个确认框。如果他们单击确定,他们将被发送到一个新的 php 函数。

I've tried:

我试过了:

function confirmReset() {
 var r = confirm('Are you sure?');
 var url = window.location.pathname;
 var pathArray = url.split('/');
 var host = pathArray[1];
 var newHost = '/headquarters/designReset';

 if (r == true) {
    window.location = host + newHost;
    //document.location.href = '/headquarters/designReset';
 } else {
    alert('it didnt work');
 }
 return false;
}?

Firebug error says:

萤火虫错误说:

SyntaxError: illegal character
}a?

语法错误:非法字符
}a?

It's saying that a? is after the } at the end of the function. I'm getting this script externally so if you post an answer make sure it works externally. Not just in the html file itself. Ive had answers that dont. Thanks!

是说a? 在函数末尾的 } 之后。我从外部获取此脚本,因此如果您发布答案,请确保它在外部运行。不仅仅是在 html 文件本身中。我有没有的答案。谢谢!

回答by VisioN

There is no stringmethod. It gives an error. Also consider adding return falseto your function in order to prevent the default link action.

没有string方法。它给出了一个错误。还可以考虑添加return false到您的函数中以防止默认链接操作。

HTML:

HTML:

<a href="#" onclick="return confirmReset();">Test</a>?

JavaScript:

JavaScript:

function confirmReset() {
    var r = confirm('Are you sure?');
    var url = window.location.pathname;
    var pathArray = url.split('/');        // <-- no need in "string()"
    var host = pathArray[1];
    var newHost = '/headquarters/designReset';

    if (r == true) {
        window.location = host + newHost;
        //document.location.href = '/headquarters/designReset';
    } else {
        alert('it didnt work');
    }
    return false;                          // <-- add this line
}?

DEMO:http://jsfiddle.net/xKhaq/

演示:http : //jsfiddle.net/xKhaq/