简体   繁体   English

html - 如何在下拉列表中获取选项标签的自定义属性?

[英]html - how to get custom attribute of option tag in dropdown?

If I have this code: 如果我有这个代码:

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

How can I get the value '-1' from the custom attribute isred ? 如何从自定义属性isred中获取值“-1”? I don't want to use the value property. 我不想使用value属性。 And I dont want to target the option tag by a name or id. 而且我不希望通过名称或ID来定位选项标记。

I want something like onchange="alert(this.getselectedoptionID.getAttribute('isred'));" 我想要像onchange="alert(this.getselectedoptionID.getAttribute('isred'));"

Can anyone help? 有人可以帮忙吗?

Also I don't want to use jquery. 另外我不想使用jquery。

You need to figure out what the selectedIndex is, then getAttribute from that options[] Array. 您需要确定selectedIndex是什么,然后从那些options [] Array中找出getAttribute

<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 DEMO jsFiddle DEMO

As a side note: 作为旁注:

Don't use inline javascript in your HTML . 不要HTML 使用内联JavaScript You want to separate your business logic from your UI. 您希望将业务逻辑与UI分开。 Create a javascript event handlers instead to handle this. 创建一个javascript事件处理程序来处理这个问题。 (jQuery / Angular / etc) (jQuery / Angular / etc)

在jquery中,你可以写:

$("#myname").find(':selected').attr('isred');

Use something like this: 使用这样的东西:

document.getElementById("x").onchange = function () {
    console.log(this.options[this.selectedIndex].getAttribute("isred"));
};

Assuming we have a HTML markup as below: 假设我们有一个HTML标记如下:

<form id="frm_">
    <select name="Veh">
        <option value='-1' selected='selected'>Select</option>
        <option value='0' ren='x'>xxx</option>
        <option value='1' ren='y'>yyy</option>
    </select>
</form>

The attr "ren" can be accessed like this: 可以像这样访问attr "ren"

function getRep() {
    var ren = document.forms['frm_'].elements['Veh'].options[document.forms['frm_']
                 .elements['Veh'].selectedIndex].getAttribute('ren');
    console.log("Val of ren " + ren); //x or y
}

Demo 演示

You use: .getAttribute('isred') 您使用: .getAttribute('isred')

You want: 你要:

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

 //Pure Javascript solution, and elegant one check if you really want to leverage the power of javascript. // Listening to a onchange event by ID attached with the select tag. document.getElementById("name_your_id").onchange = function(event) { //event.target.selectedOptions[0] have that option. as this is single selection by dropdown. this will always be 0th index :) let get_val = event.target.selectedOptions[0].getAttribute("isred"); console.log("Value from the Attribute: ", get_val) } 
  <select id="name_your_id" name="myname" class="myclass"> <option isred="423423" value="hi">One</option> <option isred="-1" value="hi">Two</option> </select> 

您可以

$(".myclass").val(function(){alert($("option",this).attr("isred"));})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 JavaScript 中获取选项标签的数据属性 - How to get data attribute of option tag in JavaScript 如何在Meteor Blaze中将所选属性添加到下拉选项标签? - How to add selected attribute to dropdown option tag in Meteor Blaze? 用于下拉选择的HTML选项标签 - HTML option tag for dropdown selection 触发“更改”事件侦听器后,如何获取“选项”标签的自定义属性? - How do I get the custom attribute of an “option” tag after firing the “change” event listener? 如何获取每个选项HTML标签的值? - How to get the values of each option HTML tag? 如何从React中选项标签上的自定义属性中检索数据 - How to retrieve data from custom attribute on option tag in React 使用Dalekjs测试工具,当Option标记中没有“值”属性时,如何在Dropdown(选择元素)中选择Option? - Using Dalekjs test tool, how to select Option in Dropdown(Select element) when there is no “value” attribute in Option tag? 如何使用data-attribute设置Select HTML tag的选项 - How to set Option of Select HTML tag using data- attribute 如何在angularjs中获取自定义标签的属性值? - How to get attribute value of a custom tag in angularjs? 如何从DropDown中的选定选项获取title属性的值 - How to get value of title attribute from a selected option in DropDown
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM