Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/464.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何调用数组';要显示的字符串_Javascript_Arrays - Fatal编程技术网

Javascript 如何调用数组';要显示的字符串

Javascript 如何调用数组';要显示的字符串,javascript,arrays,Javascript,Arrays,我有一个如下编码的下拉列表: var month=new Array(12); month[0]="January"; month[1]="February"; month[2]="March"; month[3]="April"; month[4]="May"; month[5]="June"; month[6]="July"; month[7]="August"; month[8]="September"; month[9]="October"

我有一个如下编码的下拉列表:

  var month=new Array(12);

  month[0]="January"; 
  month[1]="February";
  month[2]="March";
  month[3]="April";
  month[4]="May";
  month[5]="June";
  month[6]="July";
  month[7]="August";
  month[8]="September";
  month[9]="October";
  month[10]="November";
  month[11]="December";
我的问题是,如果用户选择了一个无效的日期(9月31日)说“9月没有31天!”,但当前它说“9月没有31天!”,我需要发出警报。代码如下:

 alert("Month "+month+" doesn't have 31 days!")
如何以字符串名称而不是数字为目标

编辑

完整更新代码:

function dispDate(dateObj) {

  //array to change numbered date to string
  var months=new Array(12);

  months[0]="January"; 
  months[1]="February";
  months[2]="March";
  months[3]="April";
  months[4]="May";
  months[5]="June";
  months[6]="July";
  months[7]="August";
  months[8]="September";
  months[9]="October";
  months[10]="November";
  months[11]="December";

  //getting month,day and year from form

  mon = months[dateObj.getMonth()];
  day   = dateObj.getDate();
  day = (day < 10) ? "0" + day : day;
  year  = dateObj.getYear();

  if (year < 2000) year += 1900;

  //the format of displaed date

  return (mon + " " + day+"," + " " + year);

}

//main function for all calculations

function isValidDate(){

  //getting values from selected options
  var SelectedDay = document.TestForm.firstDay_day.selectedIndex;
  var day = document.TestForm.firstDay_day.options[SelectedDay].value;
  var SelectedMonth = document.TestForm.firstDay_month.selectedIndex;
  var month = document.TestForm.firstDay_month.options[SelectedMonth].value;
  var SelectedYear = document.TestForm.firstDay_year.selectedIndex;
  var year = document.TestForm.firstDay_year.options[SelectedYear].value;

  //check for number of day in month  

  if ((month==4 || month==6 || month==9 || month==11) && day==31) { 
    alert("Month "+ months[month] +" doesn't have 31 days!")
    return false; 
  }

  if (month == 2) { // check for february 29th

    var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
    if (day>29 || (day==29 && !isleap)) {
      alert("February " + year + " doesn't have " + day + " days!");
      return false;
    }

  }  

  var dateStr=(month + "/" + day + "/" + year); 
函数dispDate(dateObj){
//将编号日期更改为字符串的数组
var月数=新阵列(12);
月[0]=“一月”;
月[1]=“2月”;
月[2]=“三月”;
月[3]=“4月”;
月[4]=“五月”;
月[5]=“6月”;
月[6]=“7月”;
月[7]=“8月”;
月[8]=“9月”;
月[9]=“10月”;
月[10]=“11月”;
月[11]=“12月”;
//从表单中获取月、日和年
mon=months[dateObj.getMonth()];
day=dateObj.getDate();
天=(天<10)?“0”+天:天;
year=dateObj.getYear();
如果(年份<2000)年份+=1900;
//显示日期的格式
回报率(周一+“+”日+”、“+”+年);
}
//所有计算的主要功能
函数isValidDate(){
//从所选选项中获取值
var SelectedDay=document.TestForm.firstDay\u day.selectedIndex;
var day=document.TestForm.firstDay\u day.options[SelectedDay].value;
var SelectedMonth=document.TestForm.firstDay\u month.selectedIndex;
var month=document.TestForm.firstDay\u month.options[SelectedMonth].value;
var SelectedYear=document.TestForm.firstDay\u year.selectedIndex;
var year=document.TestForm.firstDay\u year.options[SelectedYear]。值;
//检查月份中的天数
如果((月==4 | |月==6 | |月==9 | |月==11)和日==31){
警报(“月”+月[月]+“没有31天!”)
返回false;
}
如果(月==2){//检查2月29日
var isleap=(第%4年==0&&(第%100年!=0 | |第%400年==0));
如果(天>29 | |(天==29&&!isleap)){
警报(“二月+年+”没有“+天+”天!”);
返回false;
}
}  
var dateStr=(月+“/”+日+“/”+年);

按指定的方式调用它:

如果

然后

将提醒月份名称(9月份没有31天!)

编辑

根据您的评论,似乎您正在对数组列表和所选月号使用变量名
month

您需要更改。我建议将数组更改为
months

var months = new Array(12);

months[0]="January"; 
months[1]="February";
...
然后,通过所选的
月份
作为索引调用它:

alert("Month "+ months[month] +" doesn't have 31 days!")

假设数组变量名为
months
。在这种情况下,您需要使用:

alert("Month " + months[month - 1] + " doesn't have 31 days!");

由于常规月份是数字1-12,您必须用一个错误来解释关闭。

月份[8]
将替换为
九月
您的输出没有意义。您正在连接整个数组,但随后会说“第9个月没有31天!”.那么什么是月?数组还是数字?这里没有足够的代码。我怀疑他在为两个不同的事情重复使用同一个变量名:整个月列表和所选的月号。第二个使用覆盖了第一个。我想到了这一点,但我需要它是动态的,以防用户选择了另一个月。然后告诉我们你是如何处理这个月的selection@user4002418
month[selected_month_num]
这里是if语句:if((month==4 | | | month==6 | | | month==9 | | | month==11)&&day==31){alert(“month”+month+“没有31天!”)返回false;]@user4002418-那么,
month
是数组名,也是所选的数字?这不起作用。。。
alert("Month "+ months[month] +" doesn't have 31 days!")
alert("Month " + months[month - 1] + " doesn't have 31 days!");