获取另一个选择框之后的选择框的值已更改

时间:2012-05-18 12:01:06

标签: javascript html

我正在尝试实施一个闰年下拉DOB选择器。

所以我按此顺序有三个选择框:

<select id='selectYear' >
//php to populate this list
</select>

<select id='selectMonth'>
//php to populate this list
</select>

<select id='selectDate' >
//php to populate this list
</select>

我想要做的是将onChange添加到月份DDL,它获取所选值以及selectYear DDL的选定值,以便我可以填充selectDate DDL。

如何获取selectYear DDL的值,以便将其作为参数发送?

5 个答案:

答案 0 :(得分:1)

你的意思是:

<select id='selectMonth' onchange="doSometing(this.value);">
....
</select>

function doSometing(selectedMonth) {
    var year = document.getElementById("selectYear");
    var selectedYear = year.options[year.selectedIndex].value;
    console.log("My Selected Month:"+selectedMonth);
    console.log("My Selected year:"+selectedYear);
}

答案 1 :(得分:0)

关于这个主题的好link。简而言之:

var element = document.getElementById("selectYear");
var value = element.selectedIndex < 0 ? null : element.options[element.selectedIndex].value;

答案 2 :(得分:0)

如果您使用的是JQuery:

$("#selectMonth").change( function() {
  //This is the value of the selectlist
  val = $(this).val();
});

答案 3 :(得分:0)

如果您使用的是简单的javascript,则可以使用元素的“selectedIndex”属性:

在此链接上查找示例:http://www.w3schools.com/jsref/prop_select_selectedindex.asp

答案 4 :(得分:0)

你可以这样做:---

<select id='selectYear' >
        //php to populate this list
        </select>

        <select id='selectMonth'>
        //php to populate this list
        </select>

        <select id='selectDate' >
        //php to populate this list
        </select>

你的剧本: -

      jQuery('#selectYear, #selectMonth, #selectDate').change(function() {
                var selectYear = '';
                var selectMonth = '';
                var selectDate = '';
            var selectYear= jQuery('#selectYear').val();
            var selectMonth= jQuery('#selectMonth').val();
            var selectDate= jQuery('#selectDate').val();
var format= selectYear + selectMonth + selectDate;
alert(format);
            });