简体   繁体   中英

JQuery UI Autocomplete, change event doesn't fire

I've some problems with JQuery Autocomplete, my code it's like:

 var mySource = [{"label":"Value one","id":"1"},{"label":"Value two","id":"2"},{"label":"Value three","id":"3"}]; $("#txtAutocomplete").autocomplete({ source: mySource, select: function(event, ui){ if(ui.item){ //console.log('select', ui.item.label); $("#hiddenField").val(ui.item.id); return ui.item.label; } else{ //console.log('select with null value'); $("#hiddenField").val(''); } }, change: function(event, ui){ if(ui.item){ //console.log('change', ui.item.id); $("#hiddenField").val(ui.item.id); } else{ //console.log('change with null value'); $("#hiddenField").val(''); } } });
 <link href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" rel="stylesheet" type="text/css" /> <script src="https://code.jquery.com/jquery-1.11.3.js"></script> <script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script> <p> <ol> <li>Type 'Value' in the text box</li> <li>Press 'arrow down' to select the first element</li> <li>Press enter</li> <li>Keep pressed backspace to delete completely the selected item</li> <li>Press TAB</li> <li>Value in 'readonly' field is still there</li> </ol> </p> <input type="text" id="txtAutocomplete"> <input type="text" id="hiddenField" disabled="disabled"> <button>Button added just to have an element to focus on</button>

When I put the string 'value' in the editable field, autocomplete appears correctly, so I can select one value and put it in the textbox with id hiddenField . Then, if I clear the value in the textbox, I can't update the related hiddenField with a blank value, because change event doesn't fire. Why?

Steps to test snippet:

  • Write 'value' in the editable field
  • Select one value
  • Clear the selected value

hiddenField will still contain old value.

Thanks

Note: It doesn't work when I clear the field after selection but still keeping the focus on it.

Updated: I reported the bug here on bugs.jqueryui.com

When I run your code snippet, the change event does get fired each time I select an item, or when I clear the value, and the log gets printed. But the event gets fired only after I tab out after a selection, or after I click outside the auto-complete input element.

This is because, as per the documentation , the change event gets fired only when the element loses focus.

Steps that make it work:

  • Write 'test' in the editable field, and select an option
  • Tab out - this fires the change event
  • Delete the value
  • Tab out - this fires the change event

You don't have to keep the inputs in sync inside the autocomplete options. Attach a separate event handler to your text input like so:

 $("#txtAutocomplete").autocomplete({ source: ['test1', 'test2', 'test3'], select: function(event, ui){ console.log('select', ui.item.value); $("#hiddenField").val(ui.item.value); } }); $("#txtAutocomplete").on("input propertychange", function () { console.log("change", this.value); $("#hiddenField").val(this.value); });
 <link href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" rel="stylesheet" type="text/css" /> <script src="https://code.jquery.com/jquery-1.11.3.js"></script> <script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script> <input type="text" id="txtAutocomplete"> <input type="text" id="hiddenField" disabled="disabled">

I have run to this problem and there is a hack to avoid the problem. You have to force a blur but with a setTimeout

   if(ui.item){
     //console.log('select', ui.item.label);
     $("#hiddenField").val(ui.item.id);
      setTimeout(function () {
                    $(event.target).blur();                       
                });
     return ui.item.label;
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM