我可以获取文本和值,但我想获取另一个标签
this.options[this.selectedIndex].value <---will get the value
this.options[this.selectedIndex].text <---will get the text
我想从此选项获取data_id
<option value="L W12 6.3 quattro" data-id="200480476">L W12 6.3 quattro</option>
我已经尝试过:
this.options[this.selectedIndex].data-id
没有成功
很抱歉,我将添加更多信息...我在 ruby on Rails 的嵌套表单上使用它,并为其设置了一个更改事件:
<select id="shipment_vehicles_attributes_0_style" name="shipment[vehicles_attributes][0][style]" onchange="getStylesId(this.id,this.options[this.selectedIndex].data-id)"><option value="">Select A Style</option><option value="Base" data-id="9058">Base</option></select>
我可以在那里使用jquery
最佳答案
您的标签表明您正在使用 jQuery。然后你可以这样做:
var attr = $('option:selected').attr( "data-id");
关于javascript - 如何获取选项选定的数据标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30605028/