javascript HTML5 DataList 是否有 SelectedIndex?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4724713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 14:41:04  来源:igfitidea点击:

Is there a SelectedIndex for an HTML5 DataList?

javascripthtmlhtml-datalist

提问by Caio

You can pick the current optionof any selectelement:

您可以选择option任何select元素的电流:

mySelect.options[mySelect.selectedIndex]

Can I do the same with a DataList? Something like this:

我可以对 DataList 做同样的事情吗?像这样的东西:

<input id = "input" list = "datalist" type = "text" />

<datalist id = "datalist">
    <option value = "No. 1"></option>
    <option value = "No. 2"></option>
    <option value = "No. 3"></option>
</datalist>

<script>
    var datalist = document.getElementById ("datalist");
    var input = document.getElementById ("input");

    input.addEventListener ("keyup", function (event) {
        if (event.which === 13) {
            alert (datalist.options[datalist.selectedIndex]); // Example
        }
    }, false);
</script>

采纳答案by David Tang

No, the datalist element is for providing autocomplete to inputs. It is a source of data, is hidden from the user, and multiple inputs may link to it. Therefore it doesn't make sense to have a selectedIndex.

不, datalist 元素用于为输入提供自动完成功能。它是一个数据源,对用户隐藏,多个输入可能链接到它。因此,拥有一个selectedIndex.

Instead, you should simply check the .valueof the input:

相反,您应该简单地检查.value输入的 :

var datalist = document.getElementById ("datalist");
var input = document.getElementById ("input");

input.addEventListener ("keyup", function (event) {
    if (event.which === 13) {
        alert(input.value);
    }
}, false);

回答by Klaster_1

Judging by specs, datalistobject doesn't have selectedIndexproperty. But you can find it's default option, which have selected. Or compare input's value to each option value and manually find the index.

根据specs判断,datalistobject 没有selectedIndex属性。但是你可以找到它的默认选项,它有selected. 或者将输入的值与每个选项值进行比较并手动查找索引。

回答by muhyta

for (var i=0;i<datalist_id.options.length;i++)
    if (datalist_id.options[i].value == input_id.value) 
        {alert(datalist_id.options[i].innerText);break;}

回答by riopiedra

You can just add a value to the input element. This will be shown to the user as the "default" value. If the user decides to change it, i.e. delete this value from the input field, then the list in the datalist will show up:

您可以只向输入元素添加一个值。这将作为“默认”值显示给用户。如果用户决定更改它,即从输入字段中删除该值,则数据列表中的列表将显示:

<input list="browsers" name="browser" value="Internet Explorer">
<datalist id="browsers">
  <option value="Internet Explorer">
  <option value="Firefox">
  <option value="Chrome">
  <option value="Opera">
  <option value="Safari">
</datalist>

回答by pingle60

Lets say you have data attributes in the above example like this,

假设您在上面的示例中具有这样的数据属性,

<input list="browsers" name="browser" value="Internet Explorer">
<datalist id="browsers">
    <option value="Internet Explorer" data-company="Microsoft">
    <option value="Firefox" data-company="Mozilla">
    <option value="Chrome" data-company="Google/Alphabet">
    <option value="Opera" data-company="Opera">
    <option value="Safari" data-company="Apple">
</datalist>

and you want to obtain the data-company attribute of the selected item,

并且您想获取所选项目的数据公司属性,

using the loop above

使用上面的循环

for (var i=0;i<datalist_id.options.length;i++) {
    if (datalist_id.options[i].value == input_id.value) {
        // obtains the data-company attrbute
        console.log(datalist_id.options[i].getAttribute("data-company");
        alert(datalist_id.options[i].innerText);
        break;
    }
}