选择下拉列表中的onload javascript

时间:2015-07-22 03:54:57

标签: javascript jquery html date

我有3个月,月和日的下拉菜单。当我点击一年时,下拉列表工作正常,它加载了一个月,在我选择一个月后,它会加载日期。我遇到的问题是我选择年,月和日期。 例如,我选择的日期是:

year --> 1990 month-->2日期只有28天。工作正常。

但是当我改为新的一年时,月份变为1,日期仍然显示28天,它应显示31天。

我计划在body标签中包含onload,或者在select中包含onload但根本不包含。 有什么建议?

            function daysInMonth(m, y){
               return m===2?y&3||!(y%25)&&y&15?28:29:30+(m+(m>>3)&1);
            }

            var monthArray = []; 
            var dateArray = []; 
            function onYearChange() {
                monthArray =[];
                var year = document.getElementById("yeardialog").value
                    if (document.getElementById("yeardialog").value != "0"){
                        for(var i = 1 ; i<=12; i++){
                        monthArray.push(i + '');
                        dateArray.push(daysInMonth(i, year));
                    }

                var select = document.getElementById("monthdialog");
                    select.innerHTML = "";
                        for(var i = 0; i < monthArray.length; i++) {
                            var monthOptions = monthArray[i].replace('', '');
                            var el = document.createElement("option");
                                el.textContent = monthOptions + '';

                            if(monthOptions<10){
                                monthOptions = '0'+monthOptions
                            }
                                el.value = monthOptions;
                                select.appendChild(el);
                        }
            		console.log(monthArray);
    			}   
    		}

            function onMonthChange(){	
                if(dateArray.length>12){
                    dateArray.splice(0, 12);
                }
                var dates =[]; 
                var month = document.getElementById("monthdialog").value;
                var endDay = dateArray[month.replace('', '')-1];

                for(var date = 1; date<=endDay; date++){
                    dates.push(date + '');
                }

                var select = document.getElementById("datedialog");
                select.innerHTML = "";

                for(var i = 0; i < dates.length; i++) {
                    var dateOptions = dates[i].replace('', '');
                    var el = document.createElement("option");
                        el.textContent = dateOptions + '';

                    if(dateOptions<10){
                        dateOptions = '0'+dateOptions
                    }
                    el.value = dateOptions;
                    select.appendChild(el);
                }
            }
<select id="yeardialog" name="yeardialog" onchange="onYearChange()">
  <option value="">--</option>
  <option value="1990">1990</option>
  <option value="1991">1991</option>
  <option value="1992">1992</option>

</select>
<select id="monthdialog" onchange="onMonthChange()">
  <option></option>
</select>

<select id="datedialog">
  <option></option>
</select>

我想今天解决它。

2 个答案:

答案 0 :(得分:1)

O(n2)
function daysInMonth(m, y){
               return m===2?y&3||!(y%25)&&y&15?28:29:30+(m+(m>>3)&1);
            }

            var monthArray = []; 
            var dateArray = []; 
            function onYearChange() {
                monthArray =[];
                var year = document.getElementById("yeardialog").value
                    if (document.getElementById("yeardialog").value != "0"){
                        for(var i = 1 ; i<=12; i++){
                        monthArray.push(i + '');
                        dateArray.push(daysInMonth(i, year));
                    }

                var select = document.getElementById("monthdialog");
                    select.innerHTML = "";
                        for(var i = 0; i < monthArray.length; i++) {
                            var monthOptions = monthArray[i].replace('', '');
                            var el = document.createElement("option");
                                el.textContent = monthOptions + '';

                            if(monthOptions<10){
                                monthOptions = '0'+monthOptions
                            }
                                el.value = monthOptions;
                                select.appendChild(el);
                        }
                      onMonthChange();
            		console.log(monthArray);
    			}   
    		}

            function onMonthChange(){	
                if(dateArray.length>12){
                    dateArray.splice(0, 12);
                }
                var dates =[]; 
                var month = document.getElementById("monthdialog").value;
                var endDay = dateArray[month.replace('', '')-1];

                for(var date = 1; date<=endDay; date++){
                    dates.push(date + '');
                }

                var select = document.getElementById("datedialog");
                select.innerHTML = "";

                for(var i = 0; i < dates.length; i++) {
                    var dateOptions = dates[i].replace('', '');
                    var el = document.createElement("option");
                        el.textContent = dateOptions + '';

                    if(dateOptions<10){
                        dateOptions = '0'+dateOptions
                    }
                    el.value = dateOptions;
                    select.appendChild(el);
                }
            }

答案 1 :(得分:0)

 function onYearChange() {

            monthArray =[];
            var year = document.getElementById("yeardialog").value
            if (document.getElementById("yeardialog").value != "0"){
                        for(var i = 1 ; i<=12; i++){
                        monthArray.push(i + '');
                        dateArray.push(daysInMonth(i, year));
                    }

                var select = document.getElementById("monthdialog");
                    select.innerHTML = "";
                for(var i = 0; i < monthArray.length; i++) {
                 var monthOptions =monthArray[i].replace('', '');
                            var el = document.createElement("option");
                                el.textContent = monthOptions + '';

                            if(monthOptions<10){
                                monthOptions = '0'+monthOptions
                            }
                                el.value = monthOptions;
                                select.appendChild(el);
                        }
                    console.log(monthArray);
                }
                onMonthChange();
            }

call `onMonthChange();` function inside `onYearChange();` 

因为日期将在月份更改时加载。因为年度变化不会触发月变更事件,因为日期下拉列表没有加载。