简体   繁体   English

我可以覆盖jQueryUI自动完成功能中的选择选项吗

[英]Can I overwrite select options in jQueryUI autocomplete

Fiddle Example 小提琴的例子

I have an autocomplete code in my main js file. 我的主要js文件中有一个自动完成代码。 Here's an example: 这是一个例子:

var availableTags = [
    "ActionScript",
    "AppleScript",
    "Asp"
    ];
$("#tags").autocomplete({
    source: availableTags,
    select: function(event, ui) {
      return false; 
    }
});

And I have a separate js file that will only be served up on a specific page. 我有一个单独的js文件,该文件仅在特定页面上提供。 In that file, I want to overwrite the select option in the $("#tags") instance, here's an example in that seperate js file: 在该文件中,我想覆盖$("#tags")实例中的select选项,这是该单独的js文件中的示例:

$.widget( "ui.autocomplete", $.ui.autocomplete, {
   select: function (event, ui) {
     $(this).val(ui.item.label);  // to automatically populate the input box with the selected option  
  }
});

Is it possible to overwrite the select option like that in jQuery? 是否有可能像jQuery中那样覆盖选择选项?

Use the option method. 使用option方法。 All jQuery widgets have an option method that allows you to change options. 所有jQuery小部件都有一个option方法 ,该方法允许您更改选项。 And all jQuery widget methods are called like $().widgetname("method",arg1,arg2,...) 而且所有jQuery小部件方法都被称为$().widgetname("method",arg1,arg2,...)

http://jsfiddle.net/b9awbcuf/ http://jsfiddle.net/b9awbcuf/

$("#tags").autocomplete("option", "select", function (event, ui) {
    alert('changed');
    $(this).val(ui.item.label);
});

This is a very useful read: http://learn.jquery.com/jquery-ui/how-jquery-ui-works/ 这是非常有用的内容: http : //learn.jquery.com/jquery-ui/how-jquery-ui-works/

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

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