状态列表json数据进入下拉选择使用jquery
Posted
技术标签:
【中文标题】状态列表json数据进入下拉选择使用jquery【英文标题】:list of states json data into dropdown select using jquery 【发布时间】:2012-03-09 01:00:33 【问题描述】:这是正在返回的 json 数据:
("Data":"Alabama":"AL","Alaska":"AK","Arizona":"AZ","Arkansas":"AR","California":"CA","Colorado":"CO","Connecticutt":"CT","Delaware":"DE","Florida":"FL","Georgia":"GA","Hawaii":"HI","Idaho":"ID","Illinois":"IL","Indiana":"IN","Iowa":"IA","Kansas":"KS","Kentucky":"KY","Louisiana":"LA","Maine":"ME","Massachusetts":"MA","Michigan":"MI")
我需要把它放到下拉选择中。
$.getJSON(url + 'GetStateList?jsoncallback=?',
function (json)
if (!jQuery.isEmptyObject(json.Data))
//need to write logic
);
谁能帮我解决这个问题?
【问题讨论】:
相关:***.com/questions/94674/… ***.com/questions/317095/… 【参考方案1】:var data = 'jsonstring', i, select = $('select');
for(i in data)
select.append($('<option value="' + data[i][1] + '">' + data[i][0] + '</option>'));
【讨论】:
结果只返回州名和stateid的第一个字母【参考方案2】:这只是一个基本示例,请随意使用它作为起点:
<html>
<HEAD>
<TITLE>Test Example</TITLE>
</HEAD>
<BODY>
<form>
<select id="sel" />
<script>
function fill()
var obj = ("Data":"Alabama":"AL","Alaska":"AK","Arizona":"AZ","Arkansas":"AR","California":"CA","Colorado":"CO","Connecticutt":"CT","Delaware":"DE","Florida":"FL","Georgia":"GA","Hawaii":"HI","Idaho":"ID","Illinois":"IL","Indiana":"IN","Iowa":"IA","Kansas":"KS","Kentucky":"KY","Louisiana":"LA","Maine":"ME","Massachusetts":"MA","Michigan":"MI");
var s = document.getElementById('sel');
var i = 0;
for(var propertyName in obj.Data)
sel.options[i++] = new Option(propertyName, obj.Data[propertyName], true, false);
fill();
</script>
</form>
</BODY>
</HTML>
【讨论】:
以上是关于状态列表json数据进入下拉选择使用jquery的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 JSON 数据填充下拉列表作为 jQuery 中的 ajax 响应
如果下拉列表使用 Hooks 和 useState 选择了值,如何更新状态