开发者

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

开发者_JS百科

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 comma seperated values, like

Tom Wayne,tom@xyz.com
Joe Parker,joe@xyz.com
Peter Simons,peter@xyz.com

Any way to accomplish that using JQuery?

<select multiple="multiple" name="search_results">
<option  value="tom@xyz.com">Tom Wane</option>
<option value="joe@xyz.com">Joe Parker</option>
<option value="peter@xyz.com">Peter Simons</option>
</select>

Thank You


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜