html - 如何在下拉列表中获取选项标签的自定义属性? [英] html - how to get custom attribute of option tag in dropdown?

查看:26
本文介绍了html - 如何在下拉列表中获取选项标签的自定义属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有这个代码:

 <select onchange="alert('?');" name="myname" class="myclass"> 
    <option isred="-1" value="hi">click</option>
 </select>

如何从自定义属性 isred 获取值-1"?我不想使用 value 属性.而且我不想通过名称或 ID 来定位选项标签.

How can I get the value '-1' from the custom attribute isred ? I don't want to use the value property. And I dont want to target the option tag by a name or id.

我想要类似 onchange="alert(this.getselectedoptionID.getAttribute('isred'));"

有人可以帮忙吗?

我也不想使用 jquery.

Also I don't want to use jquery.

推荐答案

你需要弄清楚 selectedIndex 是什么,然后从那个 options[] 数组中getAttribute.

You need to figure out what the selectedIndex is, then getAttribute from that options[] Array.

<select onchange="alert(this.options[this.selectedIndex].getAttribute('isred'));" name="myname" class="myclass"> 
    <option isred="-1" value="hi">click</option>
    <option isred="-5" value="hi">click</option>
</select>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

jsFiddle 演示

不要在您的 HTML 中使用内联 javascript.您希望将业务逻辑与 UI 分开.创建一个 javascript 事件处理程序来处理这个.(jQuery/Angular/等)

Don't use inline javascript in your HTML. You want to separate your business logic from your UI. Create a javascript event handlers instead to handle this. (jQuery / Angular / etc)

这篇关于html - 如何在下拉列表中获取选项标签的自定义属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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