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);
精彩评论