jQuery Autocomplete verify selected value

后端 未结 4 854
遥遥无期
遥遥无期 2020-12-25 13:08

We are using the autocomplete jQuery plugin written by Jörn Zaefferer, and are trying to verify a valid option is entered.

The plugin has result() event which fires

4条回答
  •  眼角桃花
    2020-12-25 13:54

    I use a global data structure to keep track of the values that were found

    var ac_sent = {};
    

    the .result() event handler is called before the .change() event handler, so in .result( event, data, formatted ) I add the data to the structure:

    ac_sent[ data ] = true;
    

    then in the .change( event ) event handler I check to see if there is an item at ac_sent[ data ], and if there isn't any, I know that the word was not found:

    $( "#textbox" ).change( function( event ) {
    
      var data = event.target.value;
    
      if ( !ac_sent[ data ] ) {
        // result was not found in autocomplete, do something...
        ac_sent[ data ] = true;  // remember that we processed it
      }
    
      return false;
    });
    

提交回复
热议问题