2014-02-10 87 views
0

任何人都可以告诉我如何从下拉列表中选择的项目加载列表框?下拉列表中的列表框

比方说,我有一个包含“选项1 =男” &“选项2 =女” 我想要选择男性时,在列表框中显示男性雇员的列表的下拉列表。

添加了注意事项: 我想知道我怎么能做到这一点,如果员工的名字保存在文件/数据库?

+0

是ü希望这样的事情??? http://jsfiddle.net/wizam/52LyU/1/ –

+0

有类似的票证可以在这里看到:http://stackoverflow.com/questions/1085801/how-to-get-the-selected-value-下拉列表使用javascript – Tgrosk

回答

0

这是你在追求什么?

从下拉框中选择一个特定的选项将触发Ajax调用特定JSON文件。成功时,我们将文件内容显示在列表框中。 This只是一个演示。

代码示例:

$(document).ready(function() { 
     var listbox = $('#listbox'), 
      populateListbox = function(data) { 
       for (var i = 0, item; i < data.length; i++) { 
       item = data[i]; 

       var option = $('<option>').val(item.age).text(item.name); 

       listbox.append(option); 
       } 

       showListbox(); 
      }, 
      emptyListBox = function() { 
       listbox.empty(); 
      }, 
      showListbox = function() { 
       listbox.removeClass('hide'); 
      }, 
      hideListbox = function() { 
       listbox.addClass('hide'); 
      }; 

     $('#dropdown').on('change', function (e) { 
     emptyListBox(); 
     hideListbox(); 

     var selectedOption = $("option:selected", this), 
      selectedValue = this.value; 

      switch (selectedValue) { 
      case 'male': 
       $.getJSON("male.json", populateListbox); 
       break; 
      case 'female': 
       $.getJSON("female.json", populateListbox); 
       break; 
      } 
     }); 
}); 
+0

补充说明:我想知道如果员工姓名保存在文件/数据库中,我该怎么办? (我是一个初学者为noob问题抱歉) –

+0

上面的例子已经从文件中提取数据:male.json和female.json。这样做的行是:'.getJSON(“male.json”,populateListbox);''和'$ .getJSON(“female.json”,populateListbox);'。只需检查左侧面板[此处](http://plnkr.co/edit/KVQLjrhBhw3fJU9UkW6p?p=preview),即可看到2个json文件。 –

+0

要将数据保存到数据库中,您可能需要一个服务器端Web服务来公开一些端点。使用js(例如jQuery getJSON方法)消费这些端点将返回数据,然后您可以将其连接到上面的逻辑中。 –

0

我推测,你想让它异步填充另一个列表框?你需要做的就是使用JavaScript并在文档加载时调用一个函数。例如

$(document).ready(function(){ 
    exampleFucntion(); 
    }); 

这将在文档加载时调用函数“exampleFunction”。在这个函数里面,下面的代码可以放在#dropdownbox是你的HTML下拉框的ID。

$("#dropdownbox").change(exampleOnDropDownChanged); 

这又会调用一个函数,每次下拉框的索引更改时执行一个操作。在被调用函数内部,您可以将代码动态地与男性或女性员工的详细信息(或仅填充现有下拉列表)一起创建另一个下拉框。在这个函数中,你可以用'if'语句来获得男性或女性雇员,然后执行相应的代码来填充下拉菜单。这个函数也可以调用一个可以检索数据然后填充下拉菜单的PHP页面。

我所描述不需要HTML页面被刷新的方式。我还建议您阅读JavaScript和AJAX以及基本技术,例如填充下拉框。

0

我认为这是你在找什么。 DEMO HERE 选择您的性别并显示相关的列表框。

<form id="survey" action="#" method="post"> 
    <div id="section1"> 
     <label for="Gender">Select employee gender.</label> 
     <select id="Gender" onchange="selection(this);"> 
      <option value="Gender">---Select---</option> 
      <option value="Male">Male</option> 
      <option value="Female">Female</option> 
     </select> 
    </div> 
    <div id="section2" style="display:none;">Please select employee gender from above.</div> 
    <div id="section3" style="display:none;">Male Employees 
     <br /> 
     <label for="Jim">Jim</label> 
     <input type="radio" id="Jim" /> 
     <label for="Tom">Tom</label> 
     <input type="radio" id="Tom" /> 
     <label for="Sam">Sam</label> 
     <input type="radio" id="Sam" /> 
    </div> 
    <div id="section4" style="display:none;">Female Employees<br /> 
     <label for="Kate">Kate</label> 
     <input type="radio" id="Kate" /> 
     <label for="Claire">Claire</label> 
     <input type="radio" id="Claire" /> 
     <label for="Sue">Sue</label> 
     <input type="radio" id="Sue" /> 
    </div> 
</form> 

<script> 
var sections = { 
    'Gender': 'section2', 
     'Male': 'section3', 
     'Female': 'section4' 
}; 

var selection = function (select) { 

    for (var i in sections) 
    document.getElementById(sections[i]).style.display = "none"; 

    document.getElementById(sections[select.value]).style.display = "block"; 

}; 
</script> 
+0

谢谢,这anwser是我的问题的重要组成部分,我想知道如果员工名称存在于数据库中,我该如何做到这一点? –