繁体   English   中英

用户单击确定后,JavaScript提示重定向

[英]JavaScript prompt redirect after user clicks OK

我想知道在用户在提示中输入信息并单击“确定”后,如何重定向到页面,如下所示:

var Input = prompt("Message:");
if (//Button clicked, and Input not empty\\) {
    //Sanitize input
    //Redirect to https://www.MyWeb.com/?Message=INPUT
}

很多人问诸如alert()confirm() ,但我还没有发现与prompt()任何内容

此页面将为您提供帮助。

https://www.w3schools.com/jsref/met_win_prompt.asp

var Input = prompt("Message:");

if(Input.trim() != "" && Input == "your key")
{
    location.href = "/togo";
}
else
{
    alert("Fail!");
}

这是我的理解吗?

祝好运!

你可以这样尝试

    var txt;
    if (confirm("Press a button!") == true) {
        txt = "You pressed OK!";
    } else {
        txt = "You pressed Cancel!";
    }
    document.getElementById("demo").innerHTML = txt;

有提示:

   var msg = prompt("Custom Header:");
    if (msg.length > 0) { 
        console.log('Message Added ')
    } else {
        console.log('Its empty')
    }

像这样尝试。与trim()可从字符串中删除不需要的空间

 var str = prompt("Message:"); if(str == null){ console.log('pressed cancel') } else if (str.trim()) { //validate is not empty with empty space also console.log('valid') //window.location.href = "url" } else { console.log('empty') } 

我想了下面的代码:

var Text = prompt ( "Message" );
if (Text != "" && Text != null) {
    Text = Text.trim (  );
    location.href = "https://www." + Text + ".com/"
} else {
    document.write ( "Error!" );
}

它检查用户是否:

  1. 点击取消
  2. 单击确定,但不输入任何内容

location.hrefdocument.write仅用于错误测试。

暂无
暂无

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

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