简体   繁体   English

如果 select 标签的 'id' 是动态的,如何获取 select 标签的所有值?

[英]How I can get all the values of select tag if 'id' of select tag is dynamic?

Problem: I have dropdown menus (around 10 on one page and each have different id ).问题:我有下拉菜单(一页大约 10 个,每个都有不同的 id )。 I am trying to automate those control fields, where I need to set the value of dropdown menu and I am trying to implement a generic solution for all dropdown menu.我正在尝试自动化这些控制字段,我需要在其中设置下拉菜单的值,并且我正在尝试为所有下拉菜单实施通用解决方案。

For Example: For given dropdown menu, I want to set its value to Korean (ECU-KR).例如:对于给定的下拉菜单,我想将其值设置为韩语(ECU-KR)。

My Approach: To automate this dropdown, I am trying to get all the values of select tag and iterating each to compare with the require value (that I want to set in dropdown menu(Korean (ECU-KR))).我的方法:为了自动化这个下拉菜单,我试图获取 select 标签的所有值并迭代每个值以与需要的值(我想在下拉菜单中设置(韩语(ECU-KR))进行比较)。 But I stuck here.....但是我卡在这里......

Need Help In:需要帮助:

  1. How to get 'id' of select tag?如何获取 select 标签的“id”?

  2. If I get id, then How I can get all the values of a particular select tag?如果我得到 id,那么如何获取特定 select 标签的所有值?

在此处输入图像描述

在此处输入图像描述

you can fetch the element by getElementById.您可以通过 getElementById 获取元素。 the selected value can be retrieved by the value of the dropdown.可以通过下拉列表的值检索选定的值。 the options property contains array which hold all of the options found on the dropdown so you can iterate over them and get their values. options 属性包含数组,其中包含在下拉列表中找到的所有选项,因此您可以遍历它们并获取它们的值。

 function onChange(event) { const id = event.id; console.log(id); var select = document.getElementById(id); console.log("selected = " + select.value); var options= new Array(); for (i = 0; i < select.options.length; i++) { options[i] = select.options[i].value; } console.log("options = " + options) }
 <select id="57" onchange="onChange(this)"> <option value="UTF-8">UTF-8</option> <option value="ISO-859">ISO-859</option> <option value="euc-kr">euc-kr</option> </select>

Getting the id of a selector获取选择器的 id

Assuming that you already have a variable called selector , its id can be determined by selector.id .假设您已经有一个名为selector的变量,它的id可以由selector.id确定。

Getting the ids of all selects获取所有选择的 id

for (let select of document.querySelectorAll('select')) {
    //do something with select.id
}

Getting selects by id pattern通过 id 模式获取选择

Since your id contains colon, I will assume that the select tags you are interested about all have colon in their id .由于您的id包含冒号,我将假设您感兴趣的select标签在其id中都有冒号。 In this case:在这种情况下:

for (let select of document.querySelectorAll('select[id*=":"]')) {
    //Do something with select.id
}

The *= is the contains selector. *= 是包含选择器。

Getting the values of selects having some id pattern获取具有某些 id 模式的选择的值

let values = {};
for (let select of document.querySelectorAll('select[id*=":"]')) {
    values[select.id] = [];
    for (let option of select.querySelectorAll('option')) {
        values.push(option.value);
    }
}

Assuming you know the value you want to set – and with the assumption that only one <option> element exists with that value – I'd suggest not worrying about the <select> element and instead focus on the <option> :假设您知道要设置的值——并且假设只有一个<option>元素具有该值——我建议不要担心<select>元素,而是关注<option>

// select the <option> element by its value attribute:
let option = document.querySelector('option[value="ECU-KR"]'),
    // if you still have need of the <select> element:
    select = option.closest('select');
// set the selected property of the <option>:
option.selected = true;
let x = document.getElementById("57:").selectedIndex;
let values = document.getElementsByTagName("option")[x].value;```

This should do that for you.这应该为您做到这一点。

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

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