简体   繁体   English

如何添加选择元素的选项?

[英]How to add options to select element?

How can add the array data below into a select box using JavaScript or jQuery? 如何使用JavaScript或jQuery将以下数组数据添加到select框中?

 var SHIFT_Plants = [
              { Plant: 0, PlantName: "" },
              { Plant: 2737, PlantName: "PM1" },
              { Plant: 2738, PlantName: "PM2" },
              { Plant: 2739, PlantName: "SSP" },
              { Plant: 2740, PlantName: "UT1" },
              { Plant: 2741, PlantName: "UT2" },
              { Plant: 2742, PlantName: "TW1" },
              { Plant: 2743, PlantName: "TW2" },
              { Plant: 2744, PlantName: "TW3" },
              { Plant: 2745, PlantName: "TW4" },
              { Plant: 2746, PlantName: "FL1" },
              { Plant: 2747, PlantName: "FL2" },
              { Plant: 2748, PlantName: "FL3" },
              { Plant: 2749, PlantName: "FL4" },
              { Plant: 2750, PlantName: "MS1" },
              { Plant: 2751, PlantName: "MS2" },
              { Plant: 2752, PlantName: "PY1" },
              { Plant: 2753, PlantName: "PY2" },
              { Plant: 2754, PlantName: "DDX" },
              { Plant: 2755, PlantName: "DT1" },
              { Plant: 2756, PlantName: "DT2" }
        ];

Download jQuery from here 从这里下载jQuery

This is slightly faster and cleaner. 这样会更快,更干净。

 $.each(selectValues, function(key, value) {   
         $('#mySelect')
             .append($("<option></option>")
                        .attr("value",key)
                        .text(value)); 
    });

Adding multiple options 添加多个选项

var newOptions = {
    'red' : 'Red',
    'blue' : 'Blue',
    'green' : 'Green',
    'yellow' : 'Yellow'
};
var selectedOption = 'green';

var select = $('#example');
if(select.prop) {
  var options = select.prop('options');
}
else {
  var options = select.attr('options');
}
$('option', select).remove();

$.each(newOptions, function(val, text) {
    options[options.length] = new Option(text, val);
});
select.val(selectedOption);

Use jQuery as it supports a lot of functions. 使用jQuery,因为它支持很多功能。

Loop through the array with $.each and get each array item as Object. $ .each遍历数组,并将每个数组项作为Object。 Then access the Object with its keys eg Plant and PlantName to get its values. 然后使用其键(例如PlantPlantName访问对象以获取其值。

Remember to import jQuery into HTML file. 请记住将jQuery导入HTML文件。

 $(function(){ var SHIFT_Plants = [ { Plant: 0, PlantName: "test" }, { Plant: 2737, PlantName: "PM1" }, { Plant: 2738, PlantName: "PM2" }, { Plant: 2739, PlantName: "SSP" }, { Plant: 2740, PlantName: "UT1" }, { Plant: 2741, PlantName: "UT2" }, { Plant: 2742, PlantName: "TW1" }, { Plant: 2743, PlantName: "TW2" }, { Plant: 2744, PlantName: "TW3" }, { Plant: 2745, PlantName: "TW4" }, { Plant: 2746, PlantName: "FL1" }, { Plant: 2747, PlantName: "FL2" }, { Plant: 2748, PlantName: "FL3" }, { Plant: 2749, PlantName: "FL4" }, { Plant: 2750, PlantName: "MS1" }, { Plant: 2751, PlantName: "MS2" }, { Plant: 2752, PlantName: "PY1" }, { Plant: 2753, PlantName: "PY2" }, { Plant: 2754, PlantName: "DDX" }, { Plant: 2755, PlantName: "DT1" }, { Plant: 2756, PlantName: "DT2" } ]; $.each(SHIFT_Plants, function( key, value ) { var opt = "<option value="+ value.Plant +" >"+ value.PlantName +"</option>"; $('#test').append(opt); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="test"> </select> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM