How do I get the change event for a datalist?

前端 未结 6 695
轮回少年
轮回少年 2021-02-05 11:14

I am using a datalist and need to detect when the user selects something from the drop-down list. A similar question has been asked BUT I need it so that the event fires ONLY wh

相关标签:
6条回答
  • 2021-02-05 11:45

    More Optimize

    $("input").on('input', function () {
      if($('datalist').find('option[value="'+this.value+'"]')){
        //your code as per need
        alert(this.value);
      }
    });
    
    0 讨论(0)
  • 2021-02-05 11:54

    This might only be Chrome, untested anywhere else!, but I see a change event triggered when an option is selected. Normally change only happens in textfields when the field loses focus. The change event triggers before the blur event IF it's a normal non-datalist change, so we have to check both: we're looking for a change that's not immediately followed by a blur:

    var timer;
    el.addEventListener('change', function(e) {
        timer = setTimeout(function() {
            el.dispatchEvent(new CustomEvent('datalistchange'));
        }, 1);
    });
    el.addEventListener('blur', function(e) {
        clearTimeout(timer);
    });
    

    And then you can listen for the custom datalistchange event like normally. Or you can just put your specific logic instead of the dispatchEvent.

    jsFiddle here

    0 讨论(0)
  • 2021-02-05 11:59

    Optimized Solution

    $("input").on('input', function () {
        var inputValue = this.value;
        if($('datalist').find('option').filter(function(){
            return this.value == inputVal;        
        }).length) {
            //your code as per need
            alert(this.value);
        }
    });
    
    0 讨论(0)
  • 2021-02-05 12:00

    You can manually check it on change. But you need to check change of the input of datalist.

    $(document).on('change', 'input', function(){
        var options = $('datalist')[0].options;
        var val = $(this).val();
        for (var i=0;i<options.length;i++){
           if (options[i].value === val) {
              alert(val);
              break;
           }
        }
    });
    

    FIDDLE

    0 讨论(0)
  • 2021-02-05 12:04

    Please have look for your solution it's good to go. Have look for Demo

    $(document).on('change', 'input', function(){
        var optionslist = $('datalist')[0].options;
        var value = $(this).val();
        for (var x=0;x<optionslist.length;x++){
           if (optionslist[x].value === value) {
              //Alert here value
              alert(value);
              break;
           }
        }
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <input list="data">
    <datalist id="data">
        <option value='1'>Option 1 Here</option> 
        <option value='2'>Option 2 Here</option>
    </datalist>

    0 讨论(0)
  • 2021-02-05 12:06

    In browser with the inputType property on the InputEvent you can use that to filter out any unwanted onInput events. This is "insertReplacementText" on Firefox 81 and null for Chrome/Edge 86. If you need to support IE11 you will need to validate the value is valid.

    document.getElementById("browser")
      .addEventListener("input", function(event){
            if(event.inputType == "insertReplacementText" || event.inputType == null) {
              document.getElementById("output").textContent =  event.target.value;
              event.target.value = "";
        }
    })
    <label for="browser">Choose your browser from the list:</label>
    <input list="browsers" name="browser" id="browser">
    <datalist id="browsers">
      <option value="Edge">
      <option value="Firefox">
      <option value="Chrome">
      <option value="Opera">
      <option value="Safari">
    </datalist>
    <div id="output">
    </div>

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