问题描述
我正在尝试根据第一个选择框中的选择填充一个选择框.我上网查找了很多关于硬编码选项的有用信息,但是我需要从查询中获取选项(例如Coldfusion中的cfquery).我知道cfquery是服务器端的,所以我不能将它包含在我的jquery中,但是还有另一种选择吗?
I am trying to populate one select box based on the selection made in the first select box. I've looked online, and found a lot of helpful information on hard-coded options, but I need my options to come from a query (like cfquery in coldfusion). I know that a cfquery is server-side, so I cannot include it in my jquery, but is there another option?
我正在使用以下示例:
HTML:
<select id="counties">
<option> </option>
<option> somerset </option>
<option> hertfordshire </option>
</select>
<select id="towns" disabled="true">
</select>
JS:
var countyTowns = [
["Bath", "Bristol"],
["Letchworth", "Hitchin"]
];
$("#counties").change(function() {
var county = this.selectedIndex - 1;
$("#towns").empty();
if (county === -1) {
$("#towns").attr("disabled", true);
} else {
var towns = countyTowns[county];
for (var i = 0; i < towns.length; i++) {
$("#towns").append($("<option></option>").text(towns[i]));
}
$("#towns").attr("disabled", false);
}
});
我需要的是城镇是动态的,并且能够从数据库中读取.
What I would need is for towns to be dynamic, and able to be read from a database.
任何帮助将不胜感激!
谢谢
推荐答案
这里有一些示例:
http://www.9lessons.info/2010/08/dynamic-dependent-select-box-using.html (Maybe the best, start here)
http://blog.chapagain .com.np/using-jquery-ajax-populate-selection-list/
这篇关于根据另一个选择框中的选择填充一个选择框-JQuery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!