获取下拉信息的按钮

时间:2012-08-20 18:34:08

标签: javascript html css function

我创建了一个下拉列表,在javascript中用作日历。我正在尝试设置一个按钮,以便在单击时,id“日期”将显示当前选定的年,月和日。但按钮什么也没做。我相信我已正确设置该功能,但它无法正常工作,我怀疑它放置不正确。有人可以帮助编辑我的代码来使这项工作?

随意玩我的jsfiddle .. http://jsfiddle.net/FQnEy/

这是我尝试过的......

<html>
<head>
<title>Life Event Picker Calendar</title>
</head>
<body>
<div id="date">Date</div>
<div id="event">Event</div>
Life Event Picker Calendar<br>
<hr align="left" width="200px"/>
--Year ------ Month ----- Day<br>
<div id="calendar-container"></div>
<br>Event: <input type="text" name="evname" /><br />
 <button onclick="getInfo()"type="button">Click Me!</button> 

<script type="text/javascript">
function getInfo() {
    document.getElementById("date").innerHTML = sel_year.value + sel_month.value + sel_day.value;
}

(function() {
    //variables and values for years
    var yr1 = 2011, yr2 = 2012, yr3 = 2013, yr4 = 2014;
    var years = [yr1, yr2, yr3, yr4];
    //array with months and associated days
    var calendar = [
        ["January", 31],["February", 28],["March", 31],["April", 30],["May", 31],["June", 30],["July", 31],["August", 31],["September", 30],
        ["October", 31],["November", 30],["December", 31]],
        //this is the variable that accesses the content
        cont = document.getElementById('calendar-container');
    //creates the element variables for the drop downs
    var sel_year = document.createElement('select'), sel_month = document.createElement('select'), sel_day = document.createElement('select');

    function createOption(txt, val) {
        //this creates the option but it seems that it is making the value -1 than what the text node is
        var option = document.createElement('option');
        option.value = val;
        option.appendChild(document.createTextNode(txt));
        return option;
    }

    //creates the values and text drop down values for years
    function createYearOption(val) {
        var option = document.createElement('option');
        option.value = val;
        option.appendChild(document.createTextNode(val));
        return option;
    }

    //this clears any elements for days, months, years
    function clearChildren(ele) {
        while (ele.hasChildNodes()) {
            ele.removeChild(ele.lastChild);
        }
    }

    //this function is only triggered when you recalculate the months
    function recalculateDays() {
        var year_index = sel_year.value;
        var month_index = sel_month.value,
            df = document.createDocumentFragment();
        if ((month_index == 1) && (year_index % 4 == 0)) {
            for (var i = 0, l = calendar[month_index][1]; i < l + 1; i++) {
                //the first variable is what number will be displayed in the day drop down
                df.appendChild(createOption(i + 1, i + 1));
            }
        } else {
            //l is the variable for the number of days in the month from the array above ex:28, 30, 31
            for (var i = 0, l = calendar[month_index][1]; i < l; i++) {
                //the first variable is what number will be displayed in the day drop down
                df.appendChild(createOption(i + 1, i + 1));
            }
        }
        clearChildren(sel_day);
        sel_day.appendChild(df);
    }

    //this function is triggered only when you change the year
    function recalculateDays2() {
        var month_index = sel_month.value,
            df = document.createDocumentFragment();
            year_index = sel_year.value;
        //this checks to see if the month selected is Feb and the year is a leap year   
        if ((month_index == 1) && (year_index % 4 == 0)) {
            //l is the variable for the number of days in the month from the array above ex:28, 30, 31
            for (var i = 0, l = calendar[month_index][1]; i < l + 1; i++) {
                //the first variable is what number will be displayed in the day drop down
                df.appendChild(createOption(i + 1, i + 1));
            }
        //if not a leap year and not Feb give values of normal year to all months
        } else {
            for (var i = 0, l = calendar[month_index][1]; i < l; i++) {
                //the first variable is what number will be displayed in the day drop down
                df.appendChild(createOption(i + 1, i + 1));
            }
        }
        clearChildren(sel_day);
        sel_day.appendChild(df);
    }   
    //this creates the month values
    function generateMonths() {
        var df = document.createDocumentFragment();
        for(var i=0; i<calendar.length; i++) {
            df.appendChild(createOption(calendar[i][0], i));
        }
        //clears past months
        clearChildren(sel_month);
        //appends new months onto variable df
        sel_month.appendChild(df);
    }
    //this creates the year values
    function generateYears() {
        var df = document.createDocumentFragment();
        for(var i=0; i<years.length; i++) {
            df.appendChild(createYearOption(years[i]));
        }       
        //clears past months
        clearChildren(sel_year);
        //appends new months onto variable df
        sel_year.appendChild(df);
    }

    //anytime the month selector is changed this calls the function to change the days
    sel_month.onchange = recalculateDays;
    sel_year.onchange = recalculateDays2;

    //runs the months and days functions
    generateMonths();
    generateYears();
    recalculateDays();

    //this is what displays each of the individual drop downs after everything has been done to them
    cont.appendChild(sel_year);
    cont.appendChild(sel_month);
    cont.appendChild(sel_day);

}());

</script>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

您的年,月和日变量的范围是错误的。您需要将其拉出来,如 jsFiddle example

相关问题