2017-09-07 63 views
0
g_regions = ["USD", "North America", "EUR", "Europe", "GBP", "United Kingdom", "USD", "Asia & Pacific", "USD", "Australasia", "USD", "South America", "USD", "Africa"]; 

以上數值使用jQuery的是顯示在select dropdownjQuery對象數據奇偶值到在選擇奇數作爲選項值顯示,即使選擇文本

爲如:像這樣

<select> 
    <option value="USD">North America</option> 
    <option value="EUR">Europe</option> 
    <option value="GBP">United Kingdom</option> 
    <option value="USD">Asia & Pacific</option> 
    <option value="USD">Australasia</option> 
    <option value="USD">South America</option> 
    <option value="USD">Africa</option> 
</select> 

回答

3

輸出在這裏,你有一個解決方案

$('body').append('<select />'); 
 

 
var g_regions = ["USD", "North America", "EUR", "Europe", "GBP", "United Kingdom", "USD", "Asia & Pacific", "USD", "Australasia", "USD", "South America", "USD", "Africa"]; 
 

 
for(var i=0; i<g_regions.length; i=i+2){ 
 
    $('select').append(`<option value="${g_regions[i]}">${g_regions[i+1]}</option>`); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

環路通過與step increment of 2 &陣列去再附加optionselect

我用ES6版本與backtick而不是單引號或雙引號& ${}

${}用於分配值,在花括號裏面你需要一個變量。

參考: - JavaScript-Template_literals

希望這會幫助你。

+0

'$ {g_regions [i]}'的用途是什麼?它是如何解釋的?還要加上 –

+0

這是賦值或者用數值替換,它的一個'ES6'標準使用變量裏面的倒數 – Shiladitya

+0

謝謝@Shiladitya,它工作正常 – user3101664