2010-08-10 37 views
43

我想填充下拉選擇數組使用jQuery。使用jQuery填充下拉選擇數組使用jQuery

這裏是我的代碼:

 // Add the list of numbers to the drop down here 
     var numbers[] = { 1, 2, 3, 4, 5}; 
     $.each(numbers, function(val, text) { 
      $('#items').append(
       $('<option></option>').val(val).html(text) 
      );    
     // END 

但我發現了一個錯誤。每個功能都是我從這個網站下載的。

是因爲我使用的是一維數組而被轟炸嗎?我希望選項和文本都一樣。

+0

追加的項目之一,在對DOM一次添加的功能被認爲是非常緩慢的,是高度_not advised_。更具性能的方法是建立一個字符串並在循環 – 2010-08-10 04:58:06

回答

78

嘗試for循環:

var numbers = [1, 2, 3, 4, 5]; 

for (var i=0;i<numbers.length;i++){ 
    $('<option/>').val(numbers[i]).html(numbers[i]).appendTo('#items'); 
} 

更好的方法:

var numbers = [1, 2, 3, 4, 5]; 
var option = ''; 
for (var i=0;i<numbers.length;i++){ 
    option += '<option value="'+ numbers[i] + '">' + numbers[i] + '</option>'; 
} 
$('#items').append(option); 
+0

不適用於我之後追加它? – Syno 2017-02-01 15:06:53

+0

如果你正在處理的數組不是數字或不是來自安全源,你應該使用第一種技術,除了使用'.text(number [i])'而不是'.html(number [i])''。這將確保您不會冒任何注入漏洞的風險。 – webwake 2017-07-26 13:56:06

36

數組聲明有不正確的語法。請嘗試以下,而不是:

var numbers = [ 1, 2, 3, 4, 5] 

環部分似乎是正確

$.each(numbers, function(val, text) { 
      $('#items').append($('<option></option>').val(val).html(text)) 
      }); // there was also a) missing here 

由於@Reigel做似乎增添幾分更多的性能(這不是明顯對這樣的小數組)

+0

如果您有預定義的密鑰,這是最好的選擇 – RSM 2013-11-15 16:23:19

5

你也可以這樣做:

var list = $('#items')[0]; // HTMLSelectElement 
$.each(numbers, function(index, text) { 
    list.options[list.options.length] = new Option(index, text); 
}); 
0
function validateForm(){ 
    var success = true; 
    resetErrorMessages(); 
    var myArray = []; 
    $(".linkedServiceDonationPurpose").each(function(){ 
     myArray.push($(this).val()) 
    }); 

    $(".linkedServiceDonationPurpose").each(function(){ 
    for (var i = 0; i < myArray.length; i = i + 1) { 
     for (var j = i+1; j < myArray.length; j = j + 1) 
      if(myArray[i] == myArray[j] && $(this).val() == myArray[j]){ 
       $(this).next("div").html('Duplicate item selected'); 
       success=false; 
      } 
     } 
    }); 
    if (success) { 
     return true; 
    } else { 
     return false; 
    } 
    function resetErrorMessages() { 
     $(".error").each(function(){ 
      $(this).html(''); 
     });`` 
    } 
} 
0

我使用的解決方案是創建一個使用jQuery的javascript函數:

這將填充HTML頁面上的下拉對象。請讓我知道這可以優化 - 但工作正常。

function util_PopulateDropDownListAndSelect(sourceListObject, sourceListTextFieldName, targetDropDownName, valueToSelect) 
{ 
    var options = ''; 

    // Create the list of HTML Options 
    for (i = 0; i < sourceListObject.List.length; i++) 
    { 
     options += "<option value='" + sourceListObject.List[i][sourceListTextFieldName] + "'>" + sourceListObject.List[i][sourceListTextFieldName] + "</option>\r\n"; 
    } 

    // Assign the options to the HTML Select container 
    $('select#' + targetDropDownName)[0].innerHTML = options; 

    // Set the option to be Selected 
    $('#' + targetDropDownName).val(valueToSelect); 

    // Refresh the HTML Select so it displays the Selected option 
    $('#' + targetDropDownName).selectmenu('refresh') 
} 

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
2
var qty = 5; 
var option = ''; 
for (var i=1;i <= qty;i++){ 
    option += '<option value="'+ i + '">' + i + '</option>'; 
} 
$('#items').append(option); 
2

一種解決方案是創建一個採取JSON地圖自己的jQuery插件,並填充選擇它。

(function($) {  
    $.fn.fillValues = function(options) { 
     var settings = $.extend({ 
      datas : null, 
      complete : null, 
     }, options); 

     this.each(function(){ 
      var datas = settings.datas; 
      if(datas !=null) { 
       $(this).empty(); 
       for(var key in datas){ 
        $(this).append('<option value="'+key+'"+>'+datas[key]+'</option>'); 
       } 
      } 
      if($.isFunction(settings.complete)){ 
       settings.complete.call(this); 
      } 
     }); 

    } 

}(jQuery)); 

您可以通過這樣稱呼它:

$("#select").fillValues({datas:your_map,}); 

的優點是,任何地方,你也會面臨同樣的問題,你只需要調用

$("....").fillValues({datas:your_map,}); 

的Et瞧!

你可以在你的插件,只要你喜歡

相關問題