简体   繁体   English

使用jQuery自动完成功能阻止默认操作

[英]Prevent Default Action with jQuery Autocomplete

I have a jQuery Autocomplete input field on a web page. 我在网页上有一个jQuery自动完成输入字段。 Results are retrieved from a database, but in the case of no results being found, I actually have an option saying "No Results Found". 结果是从数据库中检索出来的,但是如果没有找到结果,我实际上可以选择说“没有找到结果”。 Now, if a user selects "No Results Found" option, I want the field to display the previously selected value. 现在,如果用户选择“未找到结果”选项,则我希望该字段显示先前选择的值。 Does anyone have an idea of how to do this? 有谁知道如何做到这一点?

Couple of tips, 几个技巧

  1. Show the code. 显示代码。
  2. When u mean "Previously selected value". 当您的意思是“先前选择的值”。 What if the user search something first time which is not in the database. 如果用户第一次搜索不在数据库中的内容怎么办。
  3. If you user doesn't gets the response from the database,when it is empty or null,set the -no result- 如果您没有从数据库获得响应,则当该响应为空或为null时,请设置-no result-
  4. Worst case, if you wish to show previous result,copy it to temp variable,in onchange() event. 最坏的情况是,如果希望显示以前的结果,请在onchange()事件中将其复制到temp变量。

I'm assuming you have a field with the selected item's value? 我假设您有一个带有所选项目值的字段? I also assume you know how to retrieve the value of the previously selected item? 我还假设您知道如何检索先前选择的项目的值? If not, let me know. 如果没有,请告诉我。 With those assumptions made, you could do the following in the on click event function: 基于这些假设,您可以在点击事件功能中执行以下操作:

if(selectedItem == "No Results Found")
  {
    ui.item.value = previouslySelectedItem;
  }

This is how I would do it going on the little info you provided. 这就是我将如何处理您提供的小信息。 If you give more, maybe add some code, I can be more specific. 如果您提供更多,也许添加一些代码,我可以更具体一些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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