最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Storing the option values of a select box and storing them into a variable (by comma separated values) - Stack Over

programmeradmin2浏览0评论

How can I store the list of option values from a select box into a variable, delimited by ma?

ie:

<!DOCTYPE html>

<html>

<head>
    <meta charset="utf-8">

</head>

<body>
<select id="fruits">
  <option value="orange">orange</option>
  <option value="banana">banana</option>
  <option value="kiwi">kiwi</option>
  <option value="mango">mango</option>
  <option value="pear">pear</option>
  <option value="strawberry">strawberry</option>
</select> 
</body>

</html>

The var would be the following:

var x = "orange,banana,kiwi,mango,pear,strawberry"

How can I store the list of option values from a select box into a variable, delimited by ma?

ie:

<!DOCTYPE html>

<html>

<head>
    <meta charset="utf-8">

</head>

<body>
<select id="fruits">
  <option value="orange">orange</option>
  <option value="banana">banana</option>
  <option value="kiwi">kiwi</option>
  <option value="mango">mango</option>
  <option value="pear">pear</option>
  <option value="strawberry">strawberry</option>
</select> 
</body>

</html>

The var would be the following:

var x = "orange,banana,kiwi,mango,pear,strawberry"
Share Improve this question edited Feb 9, 2019 at 12:33 Cœur 38.8k25 gold badges206 silver badges278 bronze badges asked Sep 19, 2013 at 14:05 John SmithJohn Smith 1,68711 gold badges38 silver badges52 bronze badges 1
  • 1 Do you know what a "for loop" is? – epascarello Commented Sep 19, 2013 at 14:10
Add a ment  | 

5 Answers 5

Reset to default 8

I'd suggest, at its simplest:

var select = document.getElementById('fruits'),
    opts = select.getElementsByTagName('option'),
    x = [];

for (var i = 0, len = opts.length; i < len; i++) {
    x.push(opts[i].value);
}

x = x.join();

JS Fiddle demo.

<select>s have a property called options. You can iterate it like a regular array. You can then add their values to another, simple array and use join to make a ma seperated list out of it.

// Get the select
var select = document.getElementById("fruits");

// This array will hold the values only
var values = [];

// Iterate the options
for (var i = 0; i < select.options.length; i++) {
  // Store the value inside our array
  values.push(select.options[i].value;
}

// Make a ma seperated list
var x = values.join(",");

Try:

var x = document.getElementById('fruits').options;
var values = [];
for(var i=0;i<x.length;i++){
    values.push(x[i].value);
}
var val = values.join(",");

DEMO FIDDLE

Using getElementsByTagName('option') and a for loop are your friends here.

JSFIddle Demo

var options = document.getElementsByTagName('option');
var x = new Array();

for(var i = 0; i < options.length; i++) {
    x.push(options[i].value);
}
x = x.join(', ');

try this

<select>
<script language="text/javascript">
var x = new Array("orange,banana,kiwi,mango,pear,strawberry");

for(var i=0; i<x.length; i++)
{
document.write("<option value=\""+states[i]+"\">"+states[i]+"</option>");
}
</script>
</select>

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论