在javascript中显示一周中的几天

时间:2014-04-10 13:12:01

标签: javascript

我需要 - 创建一个带有文本输入字段和按钮的HTML表单。当在文本输入字段中输入一个数字并单击该按钮时,将调用名为DayOfTheWeek()的Javascript函数。此函数使用switch语句来确定与输入的数字相对应的星期几,即如果输入的数字是1,则显示消息“It's Monday”,如果输入的数字是2,则显示消息“It's Tuesday”并且等等。如果输入的数字不在1-7之间,则会显示消息“不是一周中的有效日期”。

我的HTML

<input type="text" name="text1"/>

<input type="button" value="Click me" onclick="days(text1.value);"/>

我的java脚本

function days(dayOfTheWeek)
{
switch (dayOfTheWeek) {

case “1”: 
    alert(“It\’s Monday”);
        break;
case “2”:
        alert(“It\’s Tuesday”);
    break;

case “3”:
        alert(“It\’s Wednesday”);
    break;

case “4”:
        alert(“It\’s Thursday”);
    break;

case “5”:
        alert(“It\’s Friday”);
    break;

case “6”:
        alert(“It\’s Saturday”);
    break;

case “7”:
        alert(“It\’s Sunday”);
    break;

default:
        alert(“Not a valid day”);
    break;  

}
}

请帮助我一直在用萤火虫发出错误,说几天没有定义

2 个答案:

答案 0 :(得分:2)

function days(dayOfTheWeek) {
  var weekday=new Array(7);
  weekday[0]="Sunday";
  weekday[1]="Monday";
  weekday[2]="Tuesday";
  weekday[3]="Wednesday";
  weekday[4]="Thursday";
  weekday[5]="Friday";
  weekday[6]="Saturday";

  var n = weekday[dayOfTheWeek];
  return n;
}

答案 1 :(得分:0)

您的更新代码

<input type="text" name="text1" id="text1" />
<input type="button" value="Click me" onclick="javascript:days(parseInt(document.getElementById('text1').value));" />
<script language=javascript>
function days(dayOfTheWeek)
{
switch (dayOfTheWeek) {

case 1: 
    alert("It\’s Monday");
        break;
case 2:
        alert("It\’s Tuesday");
    break;

case 3:
        alert("It\’s Wednesday");
    break;

case 4:
        alert("It\’s Thursday");
    break;

case 5:
        alert("It\’s Friday");
    break;

case 6:
        alert("It\’s Saturday");
    break;

case 7:
        alert("It\’s Sunday");
    break;

default:
        alert("Not a valid day");
    break;  

}
}
</script>