這是返回的JSON數據:JSON數據選擇列表:只有特定的鍵
[{"ENERGYUSE":"","EMISSIONFACTOR":2.332,"NAME":"Coal","ID":812,"UNITOFMEASURE":"Metric tons"},{"ENERGYUSE":"","EMISSIONFACTOR":0.002705,"NAME":"Diesel","ID":816,"UNITOFMEASURE":"Liters"},{"ENERGYUSE":26635048.359808,"EMISSIONFACTOR":0.0005703,"NAME":"Electricity","ID":811,"UNITOFMEASURE":"KWH"},{"ENERGYUSE":"","EMISSIONFACTOR":0.002705,"NAME":"Fuel Oil","ID":814,"UNITOFMEASURE":"Liters"},{"ENERGYUSE":"","EMISSIONFACTOR":0.002328,"NAME":"Gasoline","ID":815,"UNITOFMEASURE":"Liters"},{"ENERGYUSE":"","EMISSIONFACTOR":0.002603,"NAME":"Jet Fuel","ID":818,"UNITOFMEASURE":"Liters"},{"ENERGYUSE":"","EMISSIONFACTOR":0.00269,"NAME":"Kerosene","ID":817,"UNITOFMEASURE":"Liters"},{"ENERGYUSE":"","EMISSIONFACTOR":0.001509,"NAME":"LPG\/Propane","ID":813,"UNITOFMEASURE":"Liters"},{"ENERGYUSE":523846.2564,"EMISSIONFACTOR":0.05307,"NAME":"Natural Gas","ID":810,"UNITOFMEASURE":"million BTU"},{"ENERGYUSE":"","EMISSIONFACTOR":0.285618,"NAME":"Wood","ID":819,"UNITOFMEASURE":"Kilograms"},{"ENERGYUSE":"","EMISSIONFACTOR":"","NAME":"Other","ID":808,"UNITOFMEASURE":""}]
我期待從JSON字符串只得到名稱和ID到使用jQuery選擇列表。 因此,例如填充當選擇會是這個樣子:
<select id="EnergyList" name="EnergyList">
<option value="813">Nataural Gas</option>
<option value="812">Coal</option>
etc etc...
</select>
任何幫助,這將大大appeciated.Thankyou。
我建議建立全標記,然後在單次追加它。否則,通過這種方法,我們將最終爲每個選項更新DOM。 – Ankur 2012-08-08 12:05:33
@ankur:爲了優化代碼,你確實是正確的,另外還有一個簡單的javascript'for i = 0; ...'會比'.each()'更快。 – Nope 2012-08-08 12:09:58
酷.....不知道關於jsPerf .. +1爲了啓發我們..添加了一個測試用例,它只需加入字符串就可以更快地完成... http://jsperf.com/for-vs-each -and-append-single-vs-complete/2 – Steen 2012-08-08 13:06:25