Javascript:要求输入数值,给出不同的答案

时间:2013-07-26 10:23:23

标签: javascript if-statement

快速更新!

我可能已经修好了。现在代码看起来像这样:

<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的结尾放在&lt; 0或&gt; 100我可以提示错误信息而没有链接到那两页中的页面,如果我留在页面右边?


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

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

用户输入一个数字,我想要不同的答案,这仍然会引导你到达链接(所以没有控件可以在这里发回用户)。基于他写的数字。

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

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

这是我的HTML a:

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

这是我的Javascript函数的基础,没有加载!我尝试了一个简单的Ok / cancel提示,但是我仍然无法获得if / else语句的逻辑。 这是旧代码,我重新贴上了正确的内容!              function rangeAnswers(){         var answer = prompt(“你喜欢披萨多少钱?”)         if(answer)&gt; 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 个答案:

答案 0 :(得分:1)

t没有加载,因为你有一个尾随的其他人,你的答案周围有括号,但不是整个布尔人。尝试:

<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>

答案 1 :(得分: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>

答案 2 :(得分: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;
        }
    }

答案 3 :(得分: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>