Javascript 如何使用jquery获取select标记的属性名和值

Javascript 如何使用jquery获取select标记的属性名和值,javascript,jquery,Javascript,Jquery,我需要使用jquery/javascript获取所有属性名称以及select标记中所选选项的值 <select name=student> <option id="1" marks="80" gender="f">Abc</option> <option id="2" marks="80" gender="m">def</option> <option id="3" marks="80" gender="f

我需要使用jquery/javascript获取所有属性名称以及select标记中所选选项的值

<select name=student>
    <option id="1" marks="80" gender="f">Abc</option>
    <option id="2" marks="80" gender="m">def</option>
    <option id="3" marks="80" gender="f">xyz</option>
</select>

如何获取所选选项的属性以及属性名称和值,如id=1、marks=80、gender=f

您可以使用jquery尝试以下操作:

// This represents an Attribute.
function Attribute(name, value){
   this.name = name;
   this.value = value;
}

// This is the array of element's attributes
var attributes = [];

$("#elementsId").each(function() {
    $.each(this.attributes, function() {
        if(this.specified) {
            attributes.push(new Attribute(this.name, this.value))
    }
  });
});
在下面的代码段中,我将此应用于第一个选项

$function{ //这表示一个属性。 函数Attributename,值{ this.name=名称; 这个值=值; } //这是元素属性的数组 var属性=[]; 每个功能$1.5{ $.eachthis.attributes,函数{ 如果这是指定的{ attributes.pushnew Attributethis.name,this.value } }; };
forvar i=0;这个问题的质量可以通过包含您到目前为止尝试过的代码来提高,显示您投入的研究成果。您能告诉我attributes属性的规范吗?我似乎在MDN上找不到它。此外,可能值得指出HTML属性和数据属性之间的混淆。参见g因为marks&gender似乎与DOM元素本身没有任何关系,所以它们肯定应该存储在数据中-@RGraham返回一个包含instances@Rhumborl非常感谢!Googlefail:@Rhumborl非常感谢!@RGraham Rhumborl早些时候到达: