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

javascript - Print contents of a list box as comma-separated values using JQuery - Stack Overflow

programmeradmin1浏览0评论

I have a select box defined as shown below. I want to print out the name and email address of each item in the select box as ma seperated values, like

Tom Wayne,[email protected]
Joe Parker,[email protected]
Peter Simons,[email protected]

Any way to acplish that using JQuery?

<select multiple="multiple" name="search_results">
<option  value="[email protected]">Tom Wane</option>
<option value="[email protected]">Joe Parker</option>
<option value="[email protected]">Peter Simons</option>
</select>

Thank You

I have a select box defined as shown below. I want to print out the name and email address of each item in the select box as ma seperated values, like

Tom Wayne,[email protected]
Joe Parker,[email protected]
Peter Simons,[email protected]

Any way to acplish that using JQuery?

<select multiple="multiple" name="search_results">
<option  value="[email protected]">Tom Wane</option>
<option value="[email protected]">Joe Parker</option>
<option value="[email protected]">Peter Simons</option>
</select>

Thank You

Share Improve this question edited Jan 5, 2010 at 15:03 Ken White 126k15 gold badges236 silver badges464 bronze badges asked Jan 5, 2010 at 14:59 ScottScott 134 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 10

I think is a good example to use Traversing/map:

$('select option').map(function () {
  return $(this).text() + ',' + $(this).val();
}).get().join('\n');

Try this:

$("select").each(function() {
    var options = [];
    $(this).children("option").each(function() {
        var $this = $(this);
        options.push($this.text()+ "," + $this.val());
    });
    alert(options.join("\n"));
});

This will alert you the options for each select individually.

Something like this:

var s = "";
$("select option").each(function()
{
   var $option = $(this);
   s += $option.text() + ", " + $option.val() + "\n";
});

alert(s);
发布评论

评论列表(0)

  1. 暂无评论