Perform action when clicking HTML5 datalist option

后端 未结 3 2097
梦毁少年i
梦毁少年i 2020-11-28 12:26

I\'m using a


And using AJAX to populate the list



        
相关标签:
3条回答
  • 2020-11-28 12:33

    Sorry for digging up this question, but I've had a similar problem and have a solution, that should work for you, too.

    function onInput() {
        var val = document.getElementById("input").value;
        var opts = document.getElementById('dlist').childNodes;
        for (var i = 0; i < opts.length; i++) {
          if (opts[i].value === val) {
            // An item was selected from the list!
            // yourCallbackHere()
            alert(opts[i].value);
            break;
          }
        }
      }
    <input type='text' oninput='onInput()' id='input' list='dlist' />
    
    <datalist id='dlist'>
      <option value='Value1'>Text1</option>
      <option value='Value2'>Text2</option>
    </datalist>

    This solution is derived from Stephan Mullers solution. It should work with a dynamically populated datalist as well.

    Unfortunaltely there is no way to tell whether the user clicked on an item from the datalist or selected it by pressing the tab-key or typed the whole string by hand.

    0 讨论(0)
  • 2020-11-28 12:39

    Datalist don't support click listener and OnInput is very costly, checking everytime all the list if anything change.

    What I did was using:

    document.querySelector('#inputName').addEventListener("focusout", onInput);
    

    FocusOut will be triggered everytime a client click the input text and than click anywhere else. If they clicked the text, than clicked somewhere else I assume they put the value they wanted.

    To check if the value is valid you do the same as the input:

       function onInput(e) {
            var val = document.querySelector('#inputName').value;
            options = document.getElementById('datalist').childNodes;
            for(var i = 0; i < options.length; i++) {
                 if(options[i].innerText === val) {
                      console.log(val);
                      break;
                 }
            }
        }
    
    0 讨论(0)
  • 2020-11-28 12:42

    Due to the lack of events available for <datalist> elements, there is no way to a selection from the suggestions other than watching the input's events (change, input, etc). Also see my answer here: Determine if an element was selected from HTML 5 datalist by pressing enter key

    To check if a selection was picked from the list, you should compare each change to the available options. This means the event will also fire when a user enters an exact value manually, there is no way to stop this.

    document.querySelector('input[list="items"]').addEventListener('input', onInput);
    
    function onInput(e) {
       var input = e.target,
           val = input.value;
           list = input.getAttribute('list'),
           options = document.getElementById(list).childNodes;
    
      for(var i = 0; i < options.length; i++) {
        if(options[i].innerText === val) {
          // An item was selected from the list!
          // yourCallbackHere()
          alert('item selected: ' + val);
          break;
        }
      }
    }
    <input list="items" type="text" />
    <datalist id="items">
      <option>item 1</option>
      <option>item 2</option>
    </datalist>

    0 讨论(0)
提交回复
热议问题