【发布时间】:2014-01-05 18:06:24
【问题描述】:
我正在使用jquery 通过servlet 从数据库中获取值。我脚本中的回调函数为我提供了来自数据库的原始信息。如何将这些值附加到jsp 中的选择选项。
这是我的 Retrive_country servlet 代码:
String sql1 = "SELECT * FROM state WHERE country_ref="+countryref+"
PreparedStatement pst1 = db.getConnection().prepareStatement(sql1);
ResultSet j = pst1.executeQuery();
while (j.next()) {
state_id = j.getString(1);
state = j.getString(2);
country_ref = j.getString(3);
location.setState(state);
location.setState_id(state_id);
location.setcountry_ref(country_ref);
pw.println(state_id);
pw.println(state);
pw.println(country_ref);
}
这是我的脚本:
<script>
$(document).ready(function(){
$("#country_id").change(function() {
var xyz = $("option:selected").val();
alert(xyz)
$.get("../Retrive_country?countryREF="+xyz",
{countryREF : xyz },
function(data){
console.log(data);
alert("Data: " + data);
});
});
});
</script>
这是我的 jsp:
<div class="span2 clear">
<select name="country_id" id="country_id">
<option>-select-</option>
<option id="blabbb">1</option>
<option id="blabbb">2</option>
<option id="blabbb">3</option>
</select></div>
<div class="span2 clear">
<select name="state_ref" id="state_ref">
<option ></option>
</select></div>
这是我在控制台中的输出:
所有字符串都是状态值,整数是 stateid。
我希望它们在 jsp 中单独使用。
【问题讨论】:
-
你有一些后端响应示例吗?
-
您能以 json 格式发送您的回复吗?
标签: java javascript jquery jsp servlets