jQuery 返回选定选项属性值
如果我有 html,
<select id="autoplay">
<option data-autoplay="">no</option>
<option data-autoplay="1">yes</option>
</select>
如何使用 jQuery 返回与所选选项相关的 data-autoplay
值并将其设置为变量 var autoplay = *selected value*
因此,如果我选择 no
,它不会返回任何内容,如果我选择 yes
,它会返回 1,因此 var autoplay = "1";
if I have the html
<select id="autoplay">
<option data-autoplay="">no</option>
<option data-autoplay="1">yes</option>
</select>
how using jQuery can I return the data-autoplay
value in respect to which option is selected and set it as a variable var autoplay = *selected value*
so if I select no
it returns nothing and if I select yes
it returns 1 so var autoplay = "1";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试以下代码,
或者
try following code,
or
您可以使用
attr()
方法获取属性的值:也可以使用
data('autoplay')
,它将查看data -
前缀属性带有与data-
后面的选择器(自动播放)匹配的字符串,因此在这种情况下它将查找:data-autoplay
(有点明显, 我想)。参考文献:
attr()
。data()
。:selected
选择器。You can use the
attr()
method to get the value of an attribute:It's possible to also use
data('autoplay')
, which will look atdata-
prefixed attributes with a string matching the selector (autoplay) following thedata-
, so in this case it would look for:data-autoplay
(somewhat obviously, I suppose).References:
attr()
.data()
.:selected
selector.