我不仅尝试获取选项元素的值,还尝试获取标签属性的值。关于如何做到这一点有什么想法吗?我目前正在使用它(内联)来获取选项的值:
$('select :selected').html();
我的选择看起来像这样:
<select>
<option value="John Smith" label="JS">John Smith</option
</select>
有什么想法吗?我一直在寻找过去半个小时左右的时间,一些解决方案已经接近,但我找不到任何人正在寻找标签的值(value)。
这是我认为可以回答我的问题的一个,但我认为他们追求的东西略有不同。 -How to get label of select option with jQuery?
提前致谢!
最佳答案
试试这个
$('select option:selected').attr('label');
您使用的.html()
只是给出了选项中的内容//John Smith
您需要访问标签属性..
而是使用 data-* 属性,因为 label 属性
对于选项无效
<select>
<option value="John Smith" data-label="JS">John Smith</option>
</select>
并使用 javascript 获取数据:
$('select option:selected').attr('data-label');
或者
$('select option:selected').data('label');
$('select option:selected').val(); // Get's the value..
关于jquery - 如何获取选项元素中标签属性的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12849053/