2

快速更新!

我可能已经修好了。现在代码如下所示:

<script type="text/javascript">
    function rangeAnswers() {
    var answer = prompt ("How much do you like pizza?")
    if (answer > 0 )

            confirm("That's a bit low")
            window.location="http://www.continue.com";


        if (answer > 20 )

            confirm("Quite a decent score, isn't it?")
            window.location="http://www.continue.com";

    }
</script>

它有效。在这一点上,我认为如果我将其他 if 放在 <0 或 > 100 的末尾,我可以提示错误消息并且不链接到那两个小 if 中的页面,我留在页面上对吗?


首先让我告诉你我刚开始使用 js,所以这可能有点愚蠢。

这是我需要做的。我有一个普通的 HTML 链接,其中有一个名为 on click 的函数,它会询问用户“你有多喜欢披萨(0 100)?”之类的问题。

用户输入一个数字,我想要不同的答案,这仍然会根据他写的数字将您定向到链接(因此没有控件可以在此处发回用户)。

我想要 0-25、26-50、51-75、71-100 的四个主要断点 ELSE(更低或更高的值)它只是提醒(“您指定的数字无效”)并留在页面中,以便用户可以再次单击并输入正确的值(然后没什么特别的)。

当他插入正确的值时我想要的是让我们说 0-25“你不太喜欢它”,26-50“不好但还不错”等等 + 链接。

这是我的HTML:

<h2><a href="javascript:rangeAnswers();">Click here to tell us how much you like pizza and continue!</a></h2>

这是我未加载的 Javascript 函数的基础!我尝试了一个简单的确定/取消提示并且它有效,但我仍然无法获得 if/else 语句的逻辑。 这是旧代码,我重新粘贴了正确的! function rangeAnswers() { var answer = prompt ("你喜欢披萨多少?") if (answer) > 0

        {
            alert= "That's a bit low"
            window.location="http://www.continue.com";
        }    else

            if (answer) > 20

        {
            alert= "You don't like it nor hate it"
            window.location="http://www.continue.com";
        } else

    }
</script>

我想这就是现在的全部内容,我仍在努力解决这个问题,而我正在等待回复,但现在已经有一段时间了,我无法得到我所缺少的东西。

这是更新的工作代码

它不会加载我的警报/提示。

function rangeAnswers() {
    var answer = prompt ("How much do you like pizza?")
    if (answer > 0 )

            alert= "Nice!"
            window.location="http://www.continue.com";


        if (answer > 20 )

            alert= "Nice!"
            window.location="http://www.continue.com";

    }
4

4 回答 4

1

t 没有加载,因为您有一个尾随 else,并且您在答案周围有括号,但在整个布尔值周围没有。尝试:

<script type="text/javascript">
    function rangeAnswers(){
        var answer = prompt ("How much do you like pizza?");
        if (answer > 0) {
            alert("Nice!");
            window.location.href="http://www.continue.com";
        }else if (answer > 20){
            alert("Nice!");
            window.location.href="http://www.continue.com";
        }
    }
</script>

已编辑 - 完整版:

<script type="text/javascript">
    function rangeAnswers(){
        var answer, 
            bad=false;
        while(true){
            answer = prompt('How much do you like pizza?');
            if(isNaN(answer)){
                bad=true;
            }else{
                bad=false;
                answer = parseInt(answer,10);
                if(answer>100 || answer<0){
                    bad=true;
                }else if(answer===100){
                    alert('Wow, you REALLY love pizza!');
                }else if (answer >= 75) {
                    alert('75-99!');
                }else if (answer >= 50){
                    alert('50-74');
                }else if (answer >= 25){
                    alert('25-49');
                }else{
                    alert('0-24');
                }
                if(!bad){
                    window.location.href="http://www.continue.com";
                    return;
                }
            }
            if(bad){
                alert('I need a number between 0 and 100!');
            }
        }
    }
</script>
于 2013-07-26T10:30:52.203 回答
0

您的 html 应如下所示

<head>
    <title>Title</title>
    <script src="file.js" type="text/javascript"></script>
</head>

<body>
    <h2><a href="javascript:rangeAnswers();">Click here to tell us how much you like pizza and continue!</a></h2>

</body>

</html>
于 2013-07-26T10:34:36.403 回答
0

在你的代码中试试这个

 function rangeAnswers() {
        var answer = prompt ("How much do you like pizza?")       
       if (answer<=25 && answer =>0){
            alert("That's a bit low")//you are not alerting your data properly.
            window.location.href="http://www.continue.com";//use window.location.href instead of window.location
       }
        else if (answer<=50 && answer =>25){
             alert("You don't like it nor hate it")
            window.location.href="http://www.continue.com";
        }
    }

更新:做同样事情的其他方法是使用开关盒:

function rangeAnswers() {
            var answer = prompt ("How much do you like pizza?") 
    switch(true){
          case answer<=25 && answer =>0:
                alert("That's a bit low")//you are not alerting your data properly.
                window.location.href="http://www.continue.com";//use window.location.href instead of window.location
           break;
           case answer<=50 && answer =>25:
                 alert("You don't like it nor hate it")
                window.location.href="http://www.continue.com";
            break;
        }
    }
于 2013-07-26T10:34:44.097 回答
0

我认为不正确的语法使您的过程无法进行:

<script type="text/javascript">
function rangeAnswers() {

    var answer = prompt ("How much do you like pizza?");
    answer = answer - 0;
    if(isNaN(answer))
    {
        alert("The number you specified is invalid");
    }else if (answer >= 0 && answer <=25)
    {
         alert("You don't like it very much");
         window.location="http://www.continue.com";
    }else if (answer > 25 && answer <=50)
    {
        alert("Not good but not bad");
        window.location="http://www.continue.com";
    } 
    //and so on ....
}
</script>
于 2013-07-26T10:39:36.160 回答