0

我正在尝试实现闰年下拉 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>

我想要做的甚至是在月份 DDL 中添加一个 onChange,在该 DDL 中它获取选定的值以及 selectYear DDL 的选定值,这样我就可以填充 selectDate DDL。

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

4

5 回答 5

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);
}
于 2012-05-18T12:04:52.747 回答
0

关于该主题的一个很好的链接。简单来说:

var element = document.getElementById("selectYear");
var value = element.selectedIndex < 0 ? null : element.options[element.selectedIndex].value;
于 2012-05-18T12:05:06.857 回答
0

如果您使用的是 JQuery:

$("#selectMonth").change( function() {
  //This is the value of the selectlist
  val = $(this).val();
});
于 2012-05-18T12:06:54.470 回答
0

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

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

于 2012-05-18T12:07:15.080 回答
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);
            });
于 2012-05-18T12:23:33.743 回答