0

我有美国各州的 JSON 数据如下

{"States":["Alaska","Alabama","Arkansas","American Samoa","Arizona","California","Colorado","Connecticut","District of Columbia","Delaware","Florida","Federated States of Micronesia","Georgia","Guam","Hawaii","Iowa","Idaho","Illinois","Indiana","Kansas","Kentucky","Louisiana","Massachusetts","Maryland","Maine","Marshall Islands","Michigan","Minnesota","Missouri","Northern Marianas","Mississippi","Montana","North Carolina","North Dakota","Nebraska","New Hampshire","New Jersey","New Mexico","Nevada","New York","Ohio","Oklahoma","Oregon","Pennsylvania","Puerto Rico","Palau","Rhode Island","South Carolina","South Dakota","Tennessee","Texas","Utah","Virginia","Virgin Islands","Vermont","Washington","Wisconsin","West Virginia","Wyoming","Armed Forces Europe, the Middle East, and Canada","Armed Forces Pacific","Armed Forces Americas"]}

现在在我的网络应用程序中,我反序列化 JSON 并绑定到 HTML5 下拉列表以获取关注

在此处输入图像描述

我想要达到的是;如何修改 JSON 数据,以便当我绑定到下拉列表时,值是状态代码,文本是完整的状态名称,所以像这样

  <option value="IL">Illinois</option>
4

2 回答 2

1

请注意元素的属性中有optionsand 。optionsTextdata-bind<select>

如果你的 json 是:

States:[
    { name:'Illinois', value:'IL'},
    { name:..., value:...},
    ....
]


<select data-bind="
    options: value, // IL, ... 
    optionsText: name, // Illinois, ...
    value: lead().state, 
    optionsCaption: 'Choose state...'">
</select>
于 2013-09-28T04:50:39.447 回答
0

我必须从 Blaise 的回复中做出的唯一小改动如下

  <select data-bind="
     options: states, 
     optionsValue: 'value'// IL, ... 
     optionsText: 'name', // Illinois, ...
     value: lead().state, 
     optionsCaption: 'Choose state...'">
  </select>
于 2013-09-30T20:31:48.350 回答