if语句不起作用并显示相同的结果

时间:2014-11-13 02:46:39

标签: javascript html

我正在尝试在javascript中创建一个你的占星程序,并且只输出最后一个if语句,如何修复它以便它显示它等于什么的单词?代码链接到JSFIDDLE

<body>
<center>
    <p>To find your Horoscope click the button below!</p>
<input type='button' value='Click Me!' onclick='myHoroscope();' />

    <p id="show"></p>
</center>
 <script type="text/javascript">
    function myHoroscope () {
 confirm("In order to find your Horoscope you need to type in the month you were born correctly!")
    var horoscope = prompt("What month were you born?")
    if (age = "january")
    {
        document.getElementById('show').innerHTML = "January";
    }
    if (age = "february")
    {
        document.getElementById('show').innerHTML = "february";
    }
    if (age = "march")
    {
        document.getElementById('show').innerHTML = "march";
    }
    if (age = "april")
    {
        document.getElementById('show').innerHTML = "april";
    }
    if (age = "may")
    {
        document.getElementById('show').innerHTML = "may";
    }
    if (age = "june")
    {
        document.getElementById('show').innerHTML = "june";
    }
    if (age = "july")
    {
        document.getElementById('show').innerHTML = "july";
    }
    if (age = "august")
    {
        document.getElementById('show').innerHTML = "august";
    }
    if (age = "september")
    {
        document.getElementById('show').innerHTML = "september";
    }
    if (age = "october")
    {
        document.getElementById('show').innerHTML = "You are either the Libra 10/1 - 10/22 or Scorpio";
    }
    if (age = "november")
    { 
        document.getElementById('show').innerHTML = "The Scorpio from 11/1 -11/23 or Sagittarius";
    }
    if (age = "december")
    {
        document.getElementById('show').innerHTML = "december";
    }
        }

 </script>

</body>

2 个答案:

答案 0 :(得分:0)

Here's your working fiddle

我已将=更改为==,因为==用于检查

我还将var horoscope更改为

var age = prompt("What month were you born?")

因为var horoscope不是您所在的人,而是检查&#34;对

这里是代码:

<body>
<center>
    <p>To find your Horoscope click the button below!</p>
<input type='button' value='Click Me!' onclick='myHoroscope();' />

    <p id="show"></p>
</center>
</body>
<script>
        function myHoroscope () {
 confirm("In order to find your Horoscope you need to type in the month you were born correctly!")
    var age = prompt("What month were you born?")
    if (age =="january")
    {
        document.getElementById('show').innerHTML = "January";
    }
    if (age =="february")
    {
        document.getElementById('show').innerHTML = "february";
    }
    if (age =="march")
    {
        document.getElementById('show').innerHTML = "march";
    }
    if (age =="april")
    {
        document.getElementById('show').innerHTML = "april";
    }
    if (age =="may")
    {
        document.getElementById('show').innerHTML = "may";
    }
    if (age =="june")
    {
        document.getElementById('show').innerHTML = "june";
    }
    if (age =="july")
    {
        document.getElementById('show').innerHTML = "july";
    }
    if (age =="august")
    {
        document.getElementById('show').innerHTML = "august";
    }
    if (age =="september")
    {
        document.getElementById('show').innerHTML = "september";
    }
    if (age =="october")
    {
        document.getElementById('show').innerHTML = "You are either the Libra 10/1 - 10/22 or Scorpio";
    }
    if (age =="november")
    { 
        document.getElementById('show').innerHTML = "The Scorpio from 11/1 -11/23 or Sagittarius";
    }
    if (age =="december")
    {
        document.getElementById('show').innerHTML = "december";
    }
        }

</script>

旁注:

==运算符将在执行任何必要的类型转换后比较相等性。 ===运算符不会进行转换,因此如果两个值不是同一类型===将只返回false。在这种情况下,===会更快,并且可能会返回与==不同的结果。在所有其他情况下,性能将是相同的。从这个回答:

Which equals operator (== vs ===) should be used in JavaScript comparisons?

答案 1 :(得分:0)

请参阅http://jsfiddle.net/8q7tch74/4/

if(age === "january")

的变化:

1)。 =赋值 ==在类型转换操作数后检查值的相等性 ===进行严格的相等检查

2。)你在一个地方使用星座变量,在另一个地方使用年龄,修复它。