0
我不會用jquery做很多事情,所以請原諒,如果這是一個真正的入門級問題。我有兩種選擇 - 廣泛的學術興趣類別,然後根據第一種選擇進行細化。 Firebug顯示我從函數中獲取了正確的數據,但我無法通過該數據獲得第二個選擇。相關選擇使用jquery不按預期工作
<script>
$("#general").change(function() {
$.ajaxSetup({
dataFilter: function(data, type){
return type == 'json' ? data.replace(/^(\/{2})?/, '') : data;
}
});
//get what they selected
var selected = $("option:selected",this).val();
//no matter what, clear the other DD
$("#majors").children().remove().end().append("<option value=\"\">-- Select a Majors --</option>");
//now load in new options of selected category
if(selected == "") return;
$.getJSON(remote.cfc?method=queryMajorsRemote&returnformat=json",{"cip_fam":selected}, function(res,code) {
var newoptions = "";
for (var i = 0; i < res.DATA.length; i++) {
newoptions += "<option value=\"" + res[i].id + "\">" + res[i].name + "</option>";
}
$("#majors").children().end().append(newoptions);
});
});
</script>
HTML非常簡單 - 只有兩個選擇 - 一個ID爲#general,另一個ID爲ID_majors。
響應數據的樣本是:
[{"ID":422,"NAME":"Engineering"},{"ID":426,"NAME":"Engineering - Aerospace, Aeronautical and Astronautical"}]
當你想創建元素時,你不應該使用HTML實體符號('<') - 使用實際的'<' and '>'字符。 – Pointy