Get All of One (or More) Element's Attributes with jQuery
I have a requirement to supply values from 2 select boxes to an Action Method. I'd like to know if there's a way to automatically extract all of the attributes from the selected options from each select box into a data object that I can pass to the server with my $.get?
Ideally, I could use a function like this:
http://plugins.jquery.com/node/6530
var data = $.getAttributes($('#lstFilter option:se开发者_如何学编程lected'))
this works perfectly for single element but this it's no good for multiple elements because it returns a json object that can't be appended to.
Can anyone suggest a work around or a different approach that i could take to get a result?
Thanks
Dave
You'd best do this with map()
:
var valuesArray = $("select").map(function() {
return $(this).find(":selected").val();
});
The above returns an array of values. You may need to identify the source of each value, in which case you'll need something like:
var values = {};
$("#select").each(function() {
values[$(this).attr("name")] = $(this).find(":selected").val();
});
which creates an anonymous object of all the <select>
values.
精彩评论