简体   繁体   English

如何使用javascript使用选项标签中赋予它的值来检索选项标签的文本?

[英]how to retrieve the text of option tag using the value given to it in the option tag using javascript?

1) How to retrieve the text of the single option using the value given to it in option tag ( irrespective of which option is selected ), say I have selected Pineapple but I want to get the name of some other fruit later in the program (eg: I selected Pineapple, but later in the program I want to know what is the fruit in Option tag with value=3 ie Orange here), and I want to do it using value given to it. 1)如何使用选项标签中赋予它的来检索单个选项的文本无论选择哪个选项),假设我选择了菠萝,但我想在程序稍后获取其他一些水果的名称(例如:我选择了菠萝,但后来在程序中我想知道值=3 的 Option 标签中的水果是什么,即这里的橙色),我想使用给它的值来做。 (say retrieving Orange from its value 3) Yes we have lot of options if it is the selected item but what if it is not the one which is currently selected?I want to do it using javascript . (比如说从其值 3 中检索 Orange)是的,如果它是选定的项目,我们有很多选择,但如果它不是当前选定的项目怎么办?我想使用javascript来做。

2) How to retrieve all the elements using value given to it in option tag. 2)如何使用选项标签中赋予它的值来检索所有元素。

3) And also how to sort it using the value(in option tag) given to it and store it in an array? 3)以及如何使用给定的值(在选项标签中)对其进行排序并将其存储在数组中? ( intention is not sort in alphabetical order but sort using the value given to it option tag , I should be able to store the array with the elements in the order Apple,Banana,Orange,Pineapple) 意图不是按字母顺序排序,而是使用给它选项标签的值排序,我应该能够以 Apple、Banana、Orange、Pineapple 的顺序存储包含元素的数组)

HTML STRUCTURE HTML 结构

<form>Select your favorite fruit:
    <select id="mySelect">
        <option value="4">Pineapple</option>
        <option value="2">Banana</option>
        <option value="3">Orange</option>
        <option value="1">Apple</option>
    </select>
</form>

(sorry for putting many questions together in a single place, am new to stackoverflow) (很抱歉将许多问题放在一个地方,我是 stackoverflow 的新手)

function getSelectValues(selectId){
    let option = document.getElementById(selectId);

    let valEls = option.getElementsByTagName('option');

    let values = [];

    for (var i = 0; i < valEls.length ; i++) {
        values.push(
            {
                name: valEls[i].getAttribute('name'),
                value : valEls[i].innerHTML
            }
        );
    }
    return values;
}

also to sort a select, answer has been given here: Javascript to sort contents of select element也对选择进行排序,这里给出了答案: Javascript to sort contents of select element

You get the element by its id and process the selected option:您通过其 id 获取元素并处理所选选项:

var sel = document.getElementById('mySelect');

var opt = sel.options[sel.selectedIndex];

console.log(opt.value);

console.log(opt.text);

Sp you have requested many stuff Sp 你要求了很多东西

you could just collect the UI and then later on push it to an array and do with it what ever you would like您可以只收集UI ,然后将其推送到一个数组,然后随心所欲地使用它

 let optionsUI = document.getElementsByTagName("option") let options = [] for(let i=0; i < optionsUI.length ;i++){ options.push({ "name": optionsUI.item(i).textContent, "value": optionsUI.item(i).value }) } options.sort(function(a,b){ if (b.name < a.name){ return 1 } return -1 }) console.log(options)
 <form>Select your favorite fruit: <select id="mySelect"> <option value="4">Pineapple</option> <option value="2">Banana</option> <option value="3">Orange</option> <option value="1">Apple</option> </select> </form>

Just sort the list of options and loop over it and get all the inner text只需对选项列表进行排序并遍历它并获取所有内部文本

 let a = document.getElementsByTagName('option'); var value = []; function sortList(a) { //sort the list of options for (i = 0; i < (a.length - 1); i++) { for (j = i + 1; j < (a.length); j++) { if (a[i].value > a[j].value) { a[i].parentNode.insertBefore(a[j], a[i]); } } } for (k = 0; k < a.length; k++) { value.push(a[k].innerHTML); } } sortList(a); console.log(value);
 <form>Select your favorite fruit: <select id="mySelect"> <option value="4">Pineapple</option> <option value="2">Banana</option> <option value="3">Orange</option> <option value="1">Apple</option> </select> </form>

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

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