Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/416.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 - Fatal编程技术网

Javascript 我可以根据月份更改一个月的天数吗?

Javascript 我可以根据月份更改一个月的天数吗?,javascript,Javascript,我正在创建一个表单,我希望它能询问这个人的完整生日。我正在收集他们出生的日期、月份和年份。我有两个问题: 我可以这样做吗?如果他们选择二月作为他们的出生月份,它将在“天”选择框中显示29天,但是如果他们选择三月,它将在“天”选择框中显示31天 我有一个警告框,在提交之前显示他们的信息。现在它告诉他们,出生日期:月/日/年(例如:1970年12月6日)。是否有一个代码允许我这样做,它说,年龄:{Age}(例如:年龄:34岁) 谢谢 如果您使用的是jQuery,那么您可以查看哪个将为您处理月/日情况

我正在创建一个表单,我希望它能询问这个人的完整生日。我正在收集他们出生的日期、月份和年份。我有两个问题:

  • 我可以这样做吗?如果他们选择二月作为他们的出生月份,它将在“天”选择框中显示29天,但是如果他们选择三月,它将在“天”选择框中显示31天

  • 我有一个警告框,在提交之前显示他们的信息。现在它告诉他们,出生日期:月/日/年(例如:1970年12月6日)。是否有一个代码允许我这样做,它说,年龄:{Age}(例如:年龄:34岁)


  • 谢谢

    如果您使用的是jQuery,那么您可以查看哪个将为您处理月/日情况。(和其他选择)。至于格式,您可以查看

    如果您只是使用普通JavaScript,那么您可能可以检查月份值,并根据该更新确定该月份的天数。像这样

    var month_select = document.getElementById("month");
    month_select.onchange = function(){
      var month = month_select.options[mySelect.selectedIndex].value, days;
      if(month == 2){ // Feb
        days = 28; // or 29 if you want to check for the leap year as well
      }else if(month % 2){ // if the month is "even"
        days = month <= 7 : 30 : 31; // if the month is odd and between Jan - July, then it will have 30 days, otherwise it will have 31
      }else{
        days = month <= 7 : 31 : 30; // similar to above but when months are odd
      }
    
      // update days via DOM here
      var options = month_select.options, options_length = options.length;
      if(days > options_length){ // if it has options in days then the currently selected month, then remove some
    
      }else if(days < options_length){ // if it has less, then add
    
      } // if it has the same number, then don't do anything
    }
    
    var month\u select=document.getElementById(“月”);
    month\u select.onchange=函数(){
    var month=month\u select.options[mySelect.selectedIndex]。值,天;
    如果(月==2){//2月
    天数=28;//如果您还想查看闰年,则为29天
    }如果(月份%2){//如果月份为“偶数”
    
    days=month如果您使用jQuery,那么您可以查看哪个将为您处理月/日情况(以及其他选项)。至于格式,您可以查看

    如果您只是使用普通JavaScript,那么您可能可以检查月份值,并根据该更新确定该月份的天数

    var month_select = document.getElementById("month");
    month_select.onchange = function(){
      var month = month_select.options[mySelect.selectedIndex].value, days;
      if(month == 2){ // Feb
        days = 28; // or 29 if you want to check for the leap year as well
      }else if(month % 2){ // if the month is "even"
        days = month <= 7 : 30 : 31; // if the month is odd and between Jan - July, then it will have 30 days, otherwise it will have 31
      }else{
        days = month <= 7 : 31 : 30; // similar to above but when months are odd
      }
    
      // update days via DOM here
      var options = month_select.options, options_length = options.length;
      if(days > options_length){ // if it has options in days then the currently selected month, then remove some
    
      }else if(days < options_length){ // if it has less, then add
    
      } // if it has the same number, then don't do anything
    }
    
    var month\u select=document.getElementById(“月”);
    month\u select.onchange=函数(){
    var month=month\u select.options[mySelect.selectedIndex]。值,天;
    如果(月==2){//2月
    天数=28;//如果您还想查看闰年,则为29天
    }如果(月份%2){//如果月份为“偶数”
    天=月