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

Getting the value of a select box after another select box has been changed

本文关键字:另一个 选择 获取      更新时间:2023-09-26

我正在尝试实现一个闰年下拉 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,它在其中获取所选值以及选择年份DDL的选定值,以便我可以填充选择日期DDL。

如何获取选择年份 DDL 的值,以便将其作为参数发送?

你的意思是:

<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);
}

关于该主题的良好链接。简述:

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

如果您使用的是 JQuery:

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

如果你使用的是一个简单的javascript,那么你可以使用元素的"selectedIndex"属性:

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

你可以

这样做:---

<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);
            });