2012-05-18 147 views
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> 

我想要做的就是添加一个onchange甚至一个月DDL那里得到所选择的价值,以及作为selectYear的选定值DDL,然后我可以填充selectDate DDL。

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

+0

你使用jQuery或类似的JavaScript库? – RedFilter

+0

我对jQuery并不熟悉。我有jquery-1.3.1.min.js,虽然我需要一个脚本,但我没有自己编写脚本。 – hermann

回答

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

看到的代码,你的意思是这样的 –

+0

感谢您的想法。这是最好的答案。但由于某种原因selectedYear = year.options [year.selectedIndex] .value;返回null,所以我不得不这样做:selectedYear = document.getElementById('selectYear')。value; – hermann

0

一个好的link的话题。简而言之:

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

如果您正在使用JQuery:

$("#selectMonth").change(function() { 
    //This is the value of the selectlist 
    val = $(this).val(); 
}); 
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); 
      }); 
+0

这不是弹出代码..但你可以理解你如何实现.. –

相关问题