0

有点建立在我之前在这里问过的一个问题上

我正在使用 jquery ui 自动完成来创建一个自动完成表单。在您的帮助下,我成功地将它的组合框和类别选项组合在一起。现在经过思考,我正试图将其提升到另一个层次。

我想做的是能够让组合框也搜索 optgroup 标签以及选项文本。如果文本与 optgroup 标签匹配,则会显示整个类别,但它仍会搜索选项文本。

我猜编辑需要在下面的块中进行。

 response(select.find("option").map(function() {
                    var text = $(this).text();
                    if (this.value && (!request.term || matcher.test(text))) return {
                        label: text.replace(
                        new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + $.ui.autocomplete.escapeRegex(request.term) + ")(?![^<>]*>)(?![^&;]+;)", "gi"), "<strong>$1</strong>"),
                        value: text,
                        option: this,
                        category: $(this).closest("optgroup").attr("label")
                    };              
                }).get());

jsfiddle

4

1 回答 1

0

您发现了代码的正确部分。这是一个在类别中看起来的修改版本:

response(select.find("option").map(function() {
    var text = $(this).text(),
        category = $(this).closest("optgroup").attr("label");
    if(this.value && (!request.term || matcher.test(text) || matcher.test(category))) return {
        label: text.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + $.ui.autocomplete.escapeRegex(request.term) + ")(?![^<>]*>)(?![^&;]+;)", "gi"), "<strong>$1</strong>"),
        value: text,
        option: this,
        category: category.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + $.ui.autocomplete.escapeRegex(request.term) + ")(?![^<>]*>)(?![^&;]+;)", "gi"), "<strong>$1</strong>")
    };
}).get());
  • 除了与选项文本(matcher.test(text))匹配外,我还尝试与选项组标签匹配:matcher.test(category)
  • 我还添加了一个正则表达式替换来强调类别中的匹配文本。

(我无法在更新您的 jsfiddle 示例时进行最后一次修改,但它对我正在处理的项目非常有用)

于 2012-07-16T16:48:24.527 回答