从多个下拉框中获取值

时间:2015-04-27 20:15:05

标签: javascript jquery html for-loop drop-down-menu

我想获取三个下拉框的值,以便在单击按钮时显示在其他位置。

我搜索了很多,发现有人使用day.options [day.selectedIndex] .value;它工作得很完美,但当我尝试它然后它设法拿起我试图读取值(这比我尝试这种方法之前得到的更多)但它只会返回日:日月:月年份:年份而不是每个人的实际价值。我假设问题进来因为所有下拉框中的for循环但我不知道如何解决它。

 function Age(gender){
        if (gender!==undefined){
            el1 = document.getElementById('userdata');
            el1.innerHTML += gender +"<br>";    
        }
        el1 = document.getElementById('farespage');
        var month = 1;
        var day = 1;
        var year = 2002; 
        var newHTML = "";
            newHTML +="<div id=\"ageheading\">Date of Birth</div><div id=\"agebox\"><div class=\"innerage\" onclick=\"Relationship('"+day+"')\">Day<select id=\"dropdownday\">";
        for(var day; day<=31; day++){
            newHTML +="<option value=\"day\">"+day+"</option>"};
            newHTML +="</select></div><div class=\"innerage\">Month<select id=\"dropdownmonth\">";
        for(var month; month<=12; month++){
            newHTML +="<option value=\"month\">"+month+"</option>"};
            newHTML +="</select></div><div class=\"innerage\">Year<select id=\"dropdownyear\">";
        for(var year; year>=1920; year--){
            newHTML +="<option value=\"year\">"+year+"</option>"};
            newHTML +="</select></div><button type=\"button\" onclick=\"goback('Gender')\">back</button><button type=\"button\" onclick=\"dob()\">Enter</button></div>";
            el1.innerHTML += newHTML;
    }

    function dob(){
        var day = document.getElementById("dropdownday");
        var dayoptions = day.options[day.selectedIndex].value;
        var month = document.getElementById("dropdownmonth");
        var monthoptions = month.options[month.selectedIndex].value;
        var year = document.getElementById("dropdownyear");
        var yearoptions = year.options[year.selectedIndex].value;
        var birthday = '<div>Day: ' + dayoptions + '</div>' + '<div>Month: ' + monthoptions + '</div>' + '<div>Year: ' + yearoptions + '</div>';
        el1 = document.getElementById("userdata");
        el1.innerHTML += birthday;
        window['Relationship']();
    }

1 个答案:

答案 0 :(得分:1)

您只需要在Age函数中稍作修正:

function Age(gender){
    if (gender!==undefined){
        el1 = document.getElementById('userdata');
        el1.innerHTML += gender +"<br>";    
    }
    el1 = document.getElementById('farespage');
    var month = 1;
    var day = 1;
    var year = 2002; 
    var newHTML = "";
        newHTML +="<div id=\"ageheading\">Date of Birth</div><div id=\"agebox\"><div class=\"innerage\" onclick=\"Relationship('"+day+"')\">Day<select id=\"dropdownday\">";
    for(var day; day<=31; day++){
        newHTML +="<option value=\""+day+"\">"+day+"</option>"};
        newHTML +="</select></div><div class=\"innerage\">Month<select id=\"dropdownmonth\">";
    for(var month; month<=12; month++){
        newHTML +="<option value=\""+month+"\">"+month+"</option>"};
        newHTML +="</select></div><div class=\"innerage\">Year<select id=\"dropdownyear\">";
    for(var year; year>=1920; year--){
        newHTML +="<option value=\""+year+"\">"+year+"</option>"};
        newHTML +="</select></div><button type=\"button\" onclick=\"goback('Gender')\">back</button><button type=\"button\" onclick=\"dob()\">Enter</button></div>";
        el1.innerHTML += newHTML;
}

基本上替换value = \“day \”&gt;“with:value = \”“+ day +”\“&gt;”和年月相同。