简体   繁体   中英

Jquery Autocomplete : Enter key conflicts with another function

I have a function which listens for the enter key press event on textbox . If it is pressed , the function stops propogation of the event and then sends the contents of command to the server.

function(command)
{
          if(EnterIsPressed)
             {
                     event.stopPropogation();
                     command.sendToServer(); 
             }

}

The problem arises when I use jquery autocomplete in my code . Suppose I type something in the textbox and some autocomplete hints appear. I select one of the hint and hit enter. What I want to happen is for the textbox value to be replaced by the selected hint when I hit enter. And when I hit the next enter , the value should be sent to the server. But that's not happening. When I select the value and hit enter, the value is directly caught by the above function and sent to the server instead. Is there any way to handle this conflict?

This is not a solution but an approach:

if(EnterIsPressed)
{
   if (textbox has some value) {
      event.stopPropogation();
      command.sendToServer(); 
   } else {
      //select the autosuggest value
      //fill the textbox with value
   }
}

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