Any event triggered on autocomplete?

前端 未结 8 1934
余生分开走
余生分开走 2020-11-27 15:39

I have a pretty simple form. When the user types in an input field, I want to update what they\'ve typed somewhere else on the page. This all works fine. I\'ve bound the

相关标签:
8条回答
  • 2020-11-27 15:59

    I don't think you need an event for this: this happens only once, and there is no good browser-wide support for this, as shown by @xavi 's answer.

    Just add a function after loading the body that checks the fields once for any changes in the default value, or if it's just a matter of copying a certain value to another place, just copy it to make sure it is initialized properly.

    0 讨论(0)
  • 2020-11-27 16:03

    I've realised via monitorEvents that at least in Chrome the keyup event is fired before the autocomplete input event. On a normal keyboard input the sequence is keydown input keyup, so after the input.

    What i did is then:

    let myFun = ()=>{ ..do Something };
    
    input.addEventListener('change', myFun );
    
    //fallback in case change is not fired on autocomplete
    let _k = null;
    input.addEventListener( 'keydown', (e)=>_k=e.type );
    input.addEventListener( 'keyup', (e)=>_k=e.type );
    input.addEventListener( 'input', (e)=>{ if(_k === 'keyup') myFun();})
    

    Needs to be checked with other browser, but that might be a way without intervals.

    0 讨论(0)
  • 2020-11-27 16:04

    The only sure way is to use an interval.

    Luca's answer is too complicated for me, so I created my own short version which hopefully will help someone (maybe even me from the future):

        $input.on( 'focus', function(){
            var intervalDuration = 1000, // ms
                interval = setInterval( function(){
    
                    // do your tests here
                    // ..................
    
                    // when element loses focus, we stop checking:
                    if( ! $input.is( ':focus' ) ) clearInterval( interval );  
    
                }, intervalDuration );
        } );
    

    Tested on Chrome, Mozilla and even IE.

    0 讨论(0)
  • 2020-11-27 16:08

    As Xavi explained, there's no a solution 100% cross-browser for that, so I created a trick on my own for that (5 steps to go on):
    1. I need a couple of new arrays:

    window.timeouts     = new Array();
    window.memo_values  = new Array();
    


    2. on focus on the input text I want to trigger (in your case "email", in my example "name") I set an Interval, for example using jQuery (not needed thought):

    jQuery('#name').focus(function ()
    {
        var id = jQuery(this).attr('id');
        window.timeouts[id] = setInterval('onChangeValue.call(document.getElementById("'+ id +'"), doSomething)', 500);
    });
    


    3. on blur I remove the interval: (always using jQuery not needed thought), and I verify if the value changed

    jQuery('#name').blur(function ()
    {
        var id = jQuery(this).attr('id');
        onChangeValue.call(document.getElementById(id), doSomething);
        clearInterval(window.timeouts[id]);
        delete window.timeouts[id];
    });
    


    4. Now, the main function which check changes is the following

    function onChangeValue(callback)
    {
        if (window.memo_values[this.id] != this.value)
        {
            window.memo_values[this.id] = this.value;
    
            if (callback instanceof Function)
            {
                callback.call(this);
            }
            else
            {
                eval( callback );
            }
        }
    }
    

    Important note: you can use "this" inside the above function, referring to your triggered input HTML element. An id must be specified in order to that function to work, and you can pass a function, or a function name or a string of command as a callback.


    5. Finally you can do something when the input value is changed, even when a value is selected from a autocomplete dropdown list

    function doSomething()
    {
        alert('got you! '+this.value);
    }
    

    Important note: again you use "this" inside the above function referring to the your triggered input HTML element.

    WORKING FIDDLE!!!
    I know it sounds complicated, but it isn't.
    I prepared a working fiddle for you, the input to change is named "name" so if you ever entered your name in an online form you might have an autocomplete dropdown list of your browser to test.

    0 讨论(0)
  • 2020-11-27 16:11

    I recommending using monitorEvents. It's a function provide by the javascript console in both web inspector and firebug that prints out all events that are generated by an element. Here's an example of how you'd use it:

    monitorEvents($("input")[0]);
    

    In your case, both Firefox and Opera generate an input event when the user selects an item from the autocomplete drop down. In IE7-8 a change event is produced after the user changes focus. The latest Chrome does generate a similar event.

    A detailed browser compatibility chart can be found here:
    https://developer.mozilla.org/en-US/docs/Web/Events/input

    0 讨论(0)
  • 2020-11-27 16:11

    Add "blur". works in all browsers!

    $("input").on('blur keyup change click', function () {
    
    0 讨论(0)
提交回复
热议问题