jQuery选择:如何搜索选项的标题

jQuery Chosen: How to search in title of option

本文关键字:搜索 选项 标题 选择 何搜索 jQuery      更新时间:2023-09-26

我正在使用一个选择框与jQuery选择插件的组合。我给每个选项一个值(4位代码)和一个标题(代码的描述)。

我希望插件在标题搜索以及当我在搜索框中输入的东西。目前它只对选项的内部文本起作用。

以前有人这样做过吗?或者有人知道怎么做?

谢谢:)

我遇到了你所面临的确切问题,并设法通过操纵所选插件js库来解决它。

基本上,您需要查找带有option.html的行,因为搜索过滤器正在处理option.html.

不再只处理option.html,我们现在希望它也能使用选项的title属性进行过滤,也就是选项。title

搜索带有AbstractChosen.prototype的行。winnow_results = function() {

这是函数中的原始代码:

      option.search_text = option.group ? option.label : option.html;
      if (!(option.group && !this.group_search)) {
        option.search_match = this.search_string_match(option.search_text, regex);
        if (option.search_match && !option.group) {
          results += 1;
        }
        if (option.search_match) {
          if (searchText.length) {
            startpos = option.search_text.search(zregex);
            text = option.search_text.substr(0, startpos + searchText.length) + '</em>' + option.search_text.substr(startpos + searchText.length);
            option.search_text = text.substr(0, startpos) + '<em>' + text.substr(startpos);
          }
          if (results_group != null) {
            results_group.group_match = true;
          }
        } else if ((option.group_array_index != null) && this.results_data[option.group_array_index].search_match) {
          option.search_match = true;
        }

让我们把它改成:

      option.search_text = option.group ? option.label : option.html;
      if (!(option.group && !this.group_search)) {
        option.search_match = this.search_string_match(option.html, regex) || this.search_string_match_title(option.title, regex);
        option.search_match_title = this.search_string_match_title(option.title, regex);
        if (option.search_match && !option.group) {
          results += 1;
        }
        if (option.search_match) {
          if (searchText.length) {
            if (option.search_match_title) {
                startpos = option.title.search(zregex);
            }
            else {
                startpos = option.search_text.search(zregex);
                text = option.search_text.substr(0, startpos + searchText.length) + '</em>' + option.search_text.substr(startpos + searchText.length);
                option.search_text = text.substr(0, startpos) + '<em>' + text.substr(startpos);
            }
          }
          if (results_group != null) {
            results_group.group_match = true;
          }
        } else if ((option.group_array_index != null) && this.results_data[option.group_array_index].search_match) {
          option.search_match = true;
        } 

现在寻找下面的另一段代码:

AbstractChosen.prototype.search_string_match = function(search_string, regex) {
  var part, parts, _i, _len;
  if (regex.test(search_string)) {
    return true;
  } else if (this.enable_split_word_search && (search_string.indexOf(" ") >= 0 || search_string.indexOf("[") === 0)) {
    parts = search_string.replace(/'[|']/g, "").split(" ");
    if (parts.length) {
      for (_i = 0, _len = parts.length; _i < _len; _i++) {
        part = parts[_i];
        if (regex.test(part)) {
          return true;
        }
      }
    }
  }
};

之后,您只需在中插入下面的代码:

AbstractChosen.prototype.search_string_match_title = function(search_string, regex) {
  var part, parts, _i, _len;
  if (regex.test(search_string)) {
    return true;
  } 
};