jquery ajax提交,返回list,然后遍历,刷新select
程序员文章站
2022-07-05 14:00:57
...
ajax提交,返回list,然后遍历
- function getCityBypro(item){
- $.ajax({
- type: "POST",
- url: "gtCityByProId.action",
- data: "proid=" + item.value,
- dataType:"json",
- async: false, //如果设置为false,则表示异步提交,否则表示同步提交
- timeout: 10000,
- error: function(){
- alert('Error loading XML document');
- return null;
- },
- success: function(data){
- var items = data.cityVOList;
- $.each(items, function(i, n){
- alert(items[i].cityid);
- });
- }
- });
-
}
更新
绑定某个select,使其可以修改二级select的option,但为什么不像prototype那样可以直接add option?
- $("#chiproid").change(function(){
- $.ajax({
- type: "POST",
- url: "gtCityByProid.action",
- data: "chiproid=" + this.value,
- dataType:"json",
- success: function(data){
- var items = data.cityVOList;
- if(items != null){
- $("#chicityid").empty();
- $.each(items, function(i, n){
- $("#chicityid").append("<option value='" + items[i].cityid + "'>" + items[i].cityname + "</option>");
- });
- }
- }
- });
- });
再次更新:找到用add option 的方法了。
- var option = new Option(items[i].cityname, items[i].cityid);
- $('#chicityid')[0].options.add(option);