简体   繁体   English

如何在javascript中获取选项值

[英]How to get the option value in javascript

i have this code: 我有这个代码:

<SELECT onChange="chData(this,this.value)">
<OPTION VALUE="_MIPS-LRSYSCPU">MIPS
<OPTION VALUE="_XXCEC-LRSYSCPU">% CEC
<OPTION VALUE="_nIFL-LRSYSCPU">nIFL
</SELECT>

and I need to take with javascript all the value (not the text) of the option value that I have in the HTML PAGE. 我需要使用javascript来获取HTML PAGE中选项值的所有值(不是文本)。 The problem is that In know that the value contains the word "-LRSYSCPU", but I don't know the previous part (Example _MIPS). 问题是,知道该值包含单词“-LRSYSCPU”,但我不知道前一部分(示例_MIPS)。 the second problem is that I don't have the ID in the select so how can I take the option value? 第二个问题是我在选择中没有ID,所以如何获取选项值? I have seen that people use this code: 我见过人们使用这段代码:

var e = document.getElementById("elementId");
var value = e.options[e.selectedIndex].value;
var text = e.options[e.selectedIndex].text;

but I don't have the id so i dont know how to proceed.. 但我没有身份证,所以我不知道如何继续..

I need to create an array with this three values : _MIPS-LRSYSCPU, _XXCEC-LRSYSCPU and _nIFL-LRSYSCPU. 我需要使用以下三个值创建一个数组:_MIPS-LRSYSCPU,_XXCEC-LRSYSCPU和_nIFL-LRSYSCPU。 Thanks 谢谢

If you are using only single select on the whole page then you can use - 如果您只在整个页面上使用单一选择,那么您可以使用 -

let selectTags = document.getElementsByTagName('select'); let selectTags = document.getElementsByTagName('select');

then it will return you array of HTMLCollection [select]. 然后它会返回你的HTMLCollection数组[select]。 Now you can get your select using selectTags[0], which is an object of type HTMLCollection . 现在,您可以使用selectTags [0]来获取您的选择,这是HTMLCollection类型的对象。 This object contains childNodes . 该对象包含childNodes Iterating over child nodes will give you all options and there values. 迭代子节点将为您提供所有选项和值。

To grab all the option values in the page that include the substring "LRSYSCPU" you can use this code: 要获取页面中包含子字符串“LRSYSCPU”的所有选项值,您可以使用以下代码:

 // Grab all the options in the page const options = document.querySelectorAll('option'); // `filter` out the options that have the "LRSYSCPU" substring // in their values const filtered = [...options].filter(({ value }) => value.includes('-LRSYSCPU')); // Iterate over those filtered options with `map` and return only // the values const values = filtered.map(({ value }) => value); console.log(values); 
 <select> <option value="temp">temp</option> <option value="_MIPS-LRSYSCPU">MIPS</option> <option value="_XXCEC-LRSYSCPU">% CEC</option> <option value="temp2">temp2</option> <option value="_nIFL-LRSYSCPU">nIFL</option> </select> 

Documentation 文档

You can use document.querySelectorAll() to find all options that end with -LRSYSCPU . 您可以使用document.querySelectorAll()查找以-LRSYSCPU结尾的所有选项。 You can then loop over them and test whether they're selected. 然后,您可以循环遍历它们并测试它们是否已被选中。

let options = document.querySelector("option[value$=-LRSYSCPU]");
for (let i = 0; i < options.length; i++) {
    if (options[i].selected) {
        value = options[i].value;
        text = options[i].text;
        break;
    }
}

If you use jQuery, it has a selector extension :selected , so you can do this without a loop: 如果您使用jQuery,它有一个选择器扩展名:selected ,所以您可以在没有循环的情况下执行此操作:

var option = $("option[value$=-LRSYSCPU]:selected");
var value = option.val();
var text = option.text();

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM