基于另一个下拉选择下拉列表填充

问题描述 投票:1回答:2

我有一个JSP页面两个下拉字段和我有Oracle 10g的TYPE4连接。我想是基于一个下拉选择我想要的第二个下拉应通过获取基于在一个下拉数据自动就像刷新该JSP页面或显示一个警告类似“请稍候”,从数据库中的数据得到填补。我怎样才能做到这一点在JSP,Servlet的?

<select name="dropdown1">
    <option value="<%out.println(name);%>"><%out.println(name);%></option>
</select>

我的目标是:这下面的下拉应该得到填补基地唐上述选择:

<select name="dropdown2">
    <option value="<%out.println(rollno);%>"><%out.println(rollno);%></option>
</select>

我已经找到了一个解决方案:

<body onload="setModels()">
<% // You would get your map some other way.
Map<String,List<String>> map = new TreeMap<String,List<String>>();
Connection con=null;
String vehtypeout="";

String vehtypeout1="";
try{
Class.forName("oracle.jdbc.driver.OracleDriver");


con=DriverManager.getConnection("");


      PreparedStatement ps=null;
ResultSet  rs=null;

ps=con.prepareStatement("select c1.name, c2.roll from combo1 c1 left join combo2 c2 on      c1.name=c2.name  order by name");

rs=ps.executeQuery();



while(rs.next()){

vehtypeout=rs.getString(1);

vehtypeout1=rs.getString(2);

map.put(vehtypeout, Arrays.asList((vehtypeout1)));// here i want to show multiple value of vehtypeout1 from database but only one value is coming from databse, how can i fetch multiple value?
map.put("mercedes", Arrays.asList(new String[]{"foo", "bar"}));

      }



rs.close();

ps.close();

con.close();
}

catch(Exception e){


out.println(e);

}
%>

<%! // You may wish to put this in a class
public String modelsToJavascriptList(Collection<String> items) {
StringBuilder builder = new StringBuilder();
builder.append('[');
boolean first = true;
for (String item : items) {
    if (!first) {
      builder.append(',');
    } else {
      first = false;
    }
    builder.append('\'').append(item).append('\'');
}
builder.append(']');
return builder.toString();
}

public String mfMapToString(Map<String,List<String>> mfmap) {
StringBuilder builder = new StringBuilder();
builder.append('{');
boolean first = true;
for (String mf : mfmap.keySet()) {
  if (!first) {
      builder.append(',');
  } else {
      first = false;
  }
  builder.append('\'').append(mf).append('\'');
  builder.append(" : ");
  builder.append( modelsToJavascriptList(mfmap.get(mf)) );
  }
  builder.append("};");
  return builder.toString();
  }
%>

<script>
var modelsPerManufacturer =<%= mfMapToString(map) %>
function setSelectOptionsForModels(modelArray) {
var selectBox = document.myForm.models;

for (i = selectBox.length - 1; i>= 0; i--) {
// Bottom-up for less flicker
selectBox.remove(i);
}

for (i = 0; i< modelArray.length; i++) {
 var text = modelArray[i];
  var opt = new Option(text,text, false, false);
  selectBox.add(opt);
  }
  }

  function setModels() {
  var index = document.myForm.manufacturer.selectedIndex;
  if (index == -1) {
  return;
  }

  var manufacturerOption = document.myForm.manufacturer.options[index];
  if (!manufacturerOption) {
  // Strange, the form does not have an option with given index.
  return;
  }
  manufacturer = manufacturerOption.value;

  var modelsForManufacturer = modelsPerManufacturer[manufacturer];
  if (!modelsForManufacturer) {
  // This modelsForManufacturer is not in the modelsPerManufacturer map
  return; // or alert
  }
  setSelectOptionsForModels(modelsForManufacturer);
}

function modelSelected() {
var index = document.myForm.models.selectedIndex;
if (index == -1) {
  return;
}
// alert("You selected " + document.myForm.models.options[index].value);
}
</script>
<form name="myForm">
<select onchange="setModels()" id="manufacturer">
  <% boolean first = true;
     for (String mf : map.keySet()) { %>
      <option value="<%= mf %>" <%= first ? "SELECTED" : "" %>><%= mf %></option>
  <%   first = false;
     } %>
</select>

<select onChange="modelSelected()" id="models">
  <!-- Filled dynamically by setModels -->
</select>

但我只得到一个中值辆类型超出1,其中数据库中包含多个值。我该怎么做?

java jquery ajax jsp servlets
2个回答
0
投票

使用jQuery的功能结合到“combobox1”选择框的onchange事件。

在此功能,发送Ajax请求在您的服务器(你可以使用jQuery get函数)到JSP页面。

在JSP页面中,从数据库中检索相关数据并发送回响应与数据的客户端(可能是你需要使用JSON格式)。

在jQuery的get函数,你可以添加一个回调函数后,服务器送你回的响应来执行。

内部的回调函数,编写代码来填写“combobox2”使用由服务器发送响应数据。


0
投票

你要像下面一个Ajax调用。让你的函数被调用返回的HTML字符串

"<option value='myVal'>myText</option>"

jQuery的/阿贾克斯将是:

$("#ddl1").change(function() {
     $.ajax({
          url: "URLyouwanttoaddress",
          data: "myselection=" + $("#ddl1").val();
          type:"get",
          success: function(data) {
                     $("#ddl2").html(data);
          }
      });
});
© www.soinside.com 2019 - 2024. All rights reserved.