【问题标题】:Javascript: ask for a numeric value input, give different answersJavascript:要求输入数值,给出不同的答案
【发布时间】:2013-07-26 13:53:18
【问题描述】:

快速更新!

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

<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 放在 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 语句的逻辑。 这是旧代码,我重新粘贴了正确的! 功能范围答案(){ var answer = prompt(“你喜欢披萨多少?”) 如果(答案)> 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";

    }

【问题讨论】:

  • 请添加你所有的源代码......或者这就是你所拥有的?
  • 尝试在浏览器中打开开发者控制台,例如firefox中的firebug或chrome中的开发者工具...
  • 使用浏览器中的控制台查找并修复所有语法错误,例如if (answer &gt; 0) 而不是 if (answer) &gt; 0
  • 我已经用调试器纠正了语法错误,因为正如我所说,我是 js 新手。是的,这就是我所得到的。这是一个测试页面,所以我只有带标题的 h1 和带链接的 h2,以及不起作用的小功能。
  • 添加一些关于文件结构的信息...

标签: javascript if-statement


【解决方案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>

【讨论】:

  • 谢谢。我实际上在这里和主帖中修复了代码。它有效,但不显示我的警报/提示!
  • 要获得警报,您需要alert('text');,因为它是一个函数调用,并且函数使用这些括号。
  • 现在可以了,谢谢!我已经用最后一个问题将代码更新到顶部,这是针对不正确的值(小于零,大于 100)。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

      在你的代码中试试这个

       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";
              }
          }
      

      更新: 做同样事情的其他方法是使用 switch 案例:

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

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-25
      • 2015-04-02
      • 1970-01-01
      • 2018-11-18
      • 1970-01-01
      • 2016-03-15
      • 2021-06-04
      相关资源
      最近更新 更多