如何在下拉列表中检索数组值列表

时间:2015-07-09 11:23:45

标签: javascript html

我想在下拉列表中填充值列表。

这是我尝试过的代码,但我不知道如何继续处理数组值列表。请建议我完成任务。

InstanceFactory instanceFactory = new InstanceFactory();
ExecutorService executor = Executors.newSingleThreadExecutor();
try (Instance instance = instanceFactory.create()) {
    instance.init();
    instance.doStuff();
    while ((input = reader.readNext()) != null) {
        executor.execute(new Runnable() {
            @Override
            public void run() {
                instance.put(input);
            }
        });
    }
    executor.shutdown();
    executor.awaitTermination(1, TimeUnit.MINUTES);
}

2 个答案:

答案 0 :(得分:0)

你可以使用以下更新的代码

<!DOCTYPE html><html> <head>
  <script type="text/javascript">
     function populate_dropdown(country)
     {
        e=document.forms[0].states;
         switch(country)
         {
             case "america":
                 document.forms[0].states.length = 2;
                 document.forms[0].states.disabled = false;
                 document.forms[0].states.options[0].text = 'Washington';
                 document.forms[0].states.options[0].value = 'Washington';
                 document.forms[0].states.options[1].text = 'Florida';
                 document.forms[0].states.options[1].value = 'Florida';
             // current selected value  alert(document.forms[0].states.options[e.selectedIndex].value);
             break;
             case "india":
                 document.forms[0].states.length = 2;
                 document.forms[0].states.disabled = false;
                 document.forms[0].states.options[0].text = 'Orissa';
                 document.forms[0].states.options[0].value = 'Orissa';
                 document.forms[0].states.options[1].text = 'Punjab';
                 document.forms[0].states.options[1].value = 'Punjab';
                 break;
             default:
                 document.forms[0].states.length = 1;
                 document.forms[0].states.options[0].text = 'Select a country first';
                 document.forms[0].states.disabled = true;
         }
     }
        function dropdown_selected_value(){ 
            alert(document.forms[0].states.options[e.selectedIndex].value);
        }
  </script> </head> <body>
  <form action="this.html" method="post">
     <p>
        <input id="America" type="radio" name="country" value="America" onclick="populate_dropdown('america')" /><label for="America">America</label><br />
        <input id="India" type="radio" name="country" value="India" onclick="populate_dropdown('india')" /><label for="India">India</label><br />
        <select disabled="disabled" name="states" onchange="dropdown_selected_value()">
           <option>Select a country first</option>
        </select>
     </p>
  </form> </body> </html>

答案 1 :(得分:0)

你想要这样的东西吗?

&#13;
&#13;
states = {
    "america": [
        { "text": "Washington", "value": "Washington" },
        { "text": "Florida", "value":  "Florida" },
    ],
    "india": [
        { "text": "Orissa", "value": "Orissa" },
        { "text": "Punjab", "value": "Punjab" },
    ],
};

function populate_dropdown(country) {
    if(!states[country]) {
        document.forms[0].states.options = [];
        document.forms[0].states.disabled = true;
        return;
    }
    
    document.forms[0].states.options.length = 0;
    for(var i = 0; i < states[country].length; i++) {
        var state = states[country][i];
        document.forms[0].states.options[i] = new Option(state.text, state.value, false, false);
    }
    document.forms[0].states.disabled = false;
}
&#13;
<form action="this.html" method="post">
    <p>
        <input id="America" type="radio" name="country" value="America" onclick="populate_dropdown('america')" />
        <label for="America">America</label>
        <br />
        <input id="India" type="radio" name="country" value="India" onclick="populate_dropdown('india')" />
        <label for="India">India</label>
        <br />
        <select disabled="disabled" name="states">
            <option>Select a country first</option>
        </select>
    </p>
</form>
&#13;
&#13;
&#13;