javascript - 如何在 jquery 的 ajax 自动完成中编辑此代码

标签 javascript jquery ajax jquery-autocomplete

嗨,我正在使用ajax autocomplete for jquery插件

http://www.devbridge.com/projects/autocomplete/jquery/

有一个代码

  function Autocomplete(el, options) {
    this.el = $(el);
    this.id = options.id;
    this.el.attr('autocomplete', 'off');
    this.suggestions = [];
    this.data = [];
    this.badQueries = [];
    this.selectedIndex = -1;
    this.currentValue = this.el.val();
    this.intervalId = 0;
    this.cachedResponse = [];
    this.onChangeInterval = null;
    this.ignoreValueChange = false;
    this.serviceUrl = options.serviceUrl;
    this.isLocal = false;

    //this.options.fnFormatResult = fnFormatResult();

    this.options = {
      autoSubmit: false,
      minChars: 1,
      maxHeight: 300,
      deferRequestBy: 0,
      width: 0,
      highlight: true,
      params: {}, 
      fnFormatResult: fnFormatResult,      
      delimiter: null,
      zIndex: 9999
    };
    this.initialize();
    this.setOptions(options);
  }

在那里

this.options = {
  autoSubmit: false,
  minChars: 1,
  maxHeight: 300,
  deferRequestBy: 0,
  width: 0,
  highlight: true,
  params: {}, 
  fnFormatResult: fnFormatResult,      
  delimiter: null,
  zIndex: 9999
};

它定义了一个函数来格式化结果

fnFormatResult: fnFormatResult,   

我想使用 fnFormatResult 以外的函数,例如 fnFormatResult2

我想根据传递给插件的参数更改调用函数

我需要这样做

if(param == 1){
 fnFormatResult: fnFormatResult,   
}
else if(param == 1){
 fnFormatResult: fnFormatResult2,   
}

我怎样才能做到这一点。请帮忙......................

最佳答案

嗯,我想你可以这样做:

this.options = {
  autoSubmit: false,
  minChars: 1,
  maxHeight: 300,
  deferRequestBy: 0,
  width: 0,
  highlight: true,
  params: {}, 
  fnFormatResult: function(value, data, currentValue) {
    if(param==1) {
      return fnFormatResult.call(this, value, data, currentValue);
    } else if (param==2) {
      return fnFormatResult2.call(this, value, data, currentValue);
    }
  },      
  delimiter: null,
  zIndex: 9999
};

关于javascript - 如何在 jquery 的 ajax 自动完成中编辑此代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7268206/

相关文章:

php - 使用超链接更新数据库而不刷新页面

javascript - 无法在codepen中使用es6语法

javascript - 为什么其中一些导出语句会失败?

javascript - Jasmine - 使用构造函数正确模拟对象

javascript - 我怎样才能得到点击按钮的动态值 id 或 data 属性

javascript - 如何使用 jQuery 来显示和淡出元素?

javascript - jQuery:如何将一个函数应用于所有元素,包括一些稍后通过 Ajax 加载的元素?

javascript - 一个页面上有多个 goog_report_conversion 标签 - Google Adwords 转化跟踪

javascript - Cordova:在浏览器中测试警报插件?

javascript - 如何在php中使用ajax上传多张图片