特定的选择选项数组

时间:2017-05-17 02:47:49

标签: javascript jquery

我编写了关于选择选项的代码。我想在选择localModel之后选择本地选项,而不是它应该显示。 我通过阵列取得了成功但是,我并不知道接收数组到文本。

选择local1,local2

后收到数据

Q1 = local1& Q2 = 1

  

如果选择local1,则显示town1,town2,town3关于选择local1

     

如果选择local2,则显示town1,town2,town3关于选择local2



var localsAndModels = {};
localsAndModels['local1'] = ['town1', 'town2', 'town3'];
localsAndModels['local2'] = ['town1', 'town2', 'town3', 'town4'];
localsAndModels['local3'] = ['town1', 'town2', 'town3'];

function ChangeLocalList() {

  var localList = document.getElementById("local");
  var modelList = document.getElementById("localmodel");
  var selLocal = localList.options[localList.selectedIndex].value;

  while (modelList.options.length) {
    modelList.remove(0);
  }
  var locals = localsAndModels[selLocal];
  if (locals) {
    var i;
    for (i = 0; i < locals.length; i++) {
      var local = new Option(locals[i], i);
      modelList.options.add(local);
    }
  }
}
&#13;
<div class="container">
  <form action="searchTown.do">
    <select class="btn btn-default btn-sm dropdown-toggle" name="Q0" id="local" onchange="ChangeLocalList()">
    	<option value="">--select local--</option>
    	<option value="local1">local1</option>
    	<option value="local2">local2</option>
    	<option value="local3">local3</option>
    	
    </select>
    <select class="btn btn-default btn-sm dropdown-toggle" name="Q1" id="localmodel">
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您正在获取数值,因为您设置了此选项。替换它以传递相同的文本,它应该按照你想要的方式工作。 Html Option

var local = new Option(locals[i], locals[i]);