获取所有的一个(或多个)元素的使用jQuery属性 [英] Get All of One (or More) Element's Attributes with jQuery

查看:183
本文介绍了获取所有的一个(或多个)元素的使用jQuery属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不得不从2选择框提供值的操作方法的要求。我想知道,如果有一种方法可以自动提取所有从每个选择框中选择的选项加入到数据对象的属性,我可以传递给服务器与我的$不用彷徨?

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:selected'))

这完全适用于单个元素,但这是多个元素没有好,因为它返回不能被附加到一个JSON对象。

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?

感谢

戴夫

推荐答案

你最好使用做到这一点地图()

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();
});

这将创建所有的&LT的匿名对象;选择方式>

这篇关于获取所有的一个(或多个)元素的使用jQuery属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆