leon1509 发表于 2013-1-23 02:12:32

jQuery使用Ajax填充select

function ajaxAddOptions(url, target, id, text){target.empty();$.getJSON(url, function(json){$(json).each(function(i){var x = json;target.append("<option value='" + eval("x." + id) + "'>" + eval("x." + text) + "</option>" );})});}ajaxAddOptions(ctx + "test/testSelect", $("#province"), "code", "name"); 
 
根据需要调用ajaxAddOptions函数,test/testSelect是返回json的url,返回数据的格式如下:
 

[{"class":"ArticleType","id":501,"flag":1,"memo":null,"orderNumber":2,"name":"公告","code":"bulletin"},{"class":"ArticleType","id":500,"flag":1,"memo":null,"orderNumber":1,"name":"新闻","code":"news"}] 实现上只返回

[{"name":"公告","code":"bulletin"},{"name":"新闻","code":"news"}] 这样的数据就行了!
页: [1]
查看完整版本: jQuery使用Ajax填充select