Is there a SelectedIndex for an HTML5 DataList?

后端 未结 5 1671
情深已故
情深已故 2021-02-20 03:21

You can pick the current option of any select element:

mySelect.options[mySelect.selectedIndex]

Can I do the same wit

5条回答
  •  时光说笑
    2021-02-20 04:24

    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.

    Instead, you should simply check the .value of the input:

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

提交回复
热议问题