在更改另一个选择框后获取另一个选择框的值
Getting the value of a select box after another select box has been changed
我正在尝试实现一个闰年下拉 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);
});
相关文章:
- 当用户设置另一个选择值时,如何通过 Javascript 设置另一个选择值
- 从另一个选择框并使用数据库连接填充选择框
- 进行一个选择,自动发送表单并打开另一个选择(CodeIgniter)
- 从select中选择一个选项,然后使用Codeigniter在另一个选择框上显示数据
- 将选择下拉列表发送到另一个选择下拉列表
- JQuery以基于另一个选择列表填充选择列表
- 在填充另一个选择框时动态添加选择框
- 对两个具有相同类别的选择框进行边栏,但一个值的选择不会显示在另一个选择框中
- 当选项基于另一个选择时,保持显示所选选项
- 如何根据另一个选择框的值更新另一个选择框
- 在 JQuery 中,如何将选择选项的子集从一个选择菜单复制到另一个选择菜单
- 动态选择字段,在另一个选择字段 - Laravel 5.2 和 JS 中更改后重新填充
- 在 jQuery Mobile 中禁用从另一个选择中选择
- AngularJS 绑定要选择的数据下拉列表,当另一个选择下拉列表的值发生更改时
- Javascript 在另一个选择框选项上禁用选择框
- 当从第一个选项中选择一个选项时,如何取消隐藏另一个选择字段
- 在代码点火器中数据库中另一个选择框的更改事件时更改选择框的值
- 在更改另一个选择框后获取另一个选择框的值
- 动态添加 html 选择上单击另一个选择框选项
- 使用 jQuery 根据另一个选择框制作所需的选择框