简体   繁体   English

使用包含逗号分隔值的变量填充下拉框

[英]Populating a drop down box with a variable containing comma seperated values

How can I, populate a select box based on a variable that will contain data by comma separated values? 如何基于将包含数据(以逗号分隔的值)的变量填充选择框?

Example: 例:

var x = Jenny,John,Stephanie,Marc,Ryan,Jessica

Expected result: 预期结果:

[DROP DOWN BOX]
Jenny
John
Stephanie
Marc
Ryan
Jessica

http://jsfiddle.net/fYN7M/ http://jsfiddle.net/fYN7M/

HTML 的HTML

<select id="myoptions">

</select>

JavaScript 的JavaScript

var x = "Jenny,John,Stephanie,Marc,Ryan,Jessica";

var options = x.split(",");

var select = document.getElementById('myoptions');
for(var i=0; i<options.length; i++)
{
  select.options[i] = new Option(options[i], i);  //new Option("Text", "Value")
}

Something like this should work: 这样的事情应该起作用:

<select id="DropDownList">

</select>

<script type="text/javascript" language="javascript">
   var x = "Jenny,John,Stephanie,Marc,Ryan,Jessica";
   var splitValues = x.split(",");
   for (var i = 0; i < splitValues.length; i++) {
      var opt = document.createElement("option");

      // Add an Option object to Drop Down/List Box
      document.getElementById("DropDownList").options.add(opt);

      // Assign text and value to Option object
      opt.text = splitValues[i];
      opt.value = splitValues[i];
   }
</script>

Here's some pure javascript for you: 这是为您准备的一些纯JavaScript:

var x = 'Jenny,John,Stephanie,Marc,Ryan,Jessica';
x = x.split(',');

select = document.createElement('select');

for (var i=0;i<x.length;i++) { 
    var new_option_element = new Option(x[i], x[i]);
    select.appendChild(new_option_element);
}

document.body.appendChild(select);

http://jsfiddle.net/crowjonah/ZwtUF/1/ http://jsfiddle.net/crowjonah/ZwtUF/1/

JavaScript 的JavaScript

var x = 'Jenny,John,Stephanie,Marc,Ryan,Jessica'.split(',');
for (var i=0; i<x.length; i++) {
    document.getElementById("names").options[i] = new Option(x[i], x[i]);
}

HTML 的HTML

<select id="names"></select>

Demo 演示版

http://jsfiddle.net/hyA96/ http://jsfiddle.net/hyA96/

A simple means of this is: 一个简单的方法是:

function populateWithChildren(parent, childTag, values) {
  if (!parent || !values) {
    // no parent element, or no values, nothing can be done so quit here
    return false;
  } else {
    // this ensures that 'values' is an array:
        // if values is a string, it splits that string (assuming a comma delimiter),
        // and split() returns an array, otherwise:
        // we assume it's already an array and use that.
    values = typeof values === "string" ? values.split(',') : values;
    // iterates through all the values in the 'values' array,
    for (var i = 0, len = values.length; i < len; i++) {
      // creates a new element (as passed in the 'childTag' variable)
      var newElem = document.createElement(childTag),
          text = document.createTextNode(values[i]);
      // appends the textNode to the created element
      newElem.appendChild(text);
      // appends the created-element to the 'parent' node passed to the function
      parent.appendChild(newElem);
    }
  }
}

var parent = document.getElementById('select'),
  x = 'Jenny,John,Stephanie,Marc,Ryan,Jessica';

populateWithChildren(parent, 'option', x);

JS Fiddle demo . JS小提琴演示

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

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