自动完成多个值,保留LineBreak

时间:2013-03-23 12:05:11

标签: jquery jquery-ui jquery-autocomplete multiple-value

我正在使用jQuery UI示例中的代码来获取多个值。当我尝试换行时,它会让我,但是当我尝试添加一个新项时,它会删除换行符。我怎样才能保留换行符?

参考。 http://jqueryui.com/autocomplete/#multiple

    function split(val) {
        return val.split(/\s\s*/);
    }
    function extractLast(term) {
        return split(term).pop();
    }
    function formatItem(item) {
        return item.substr(item.indexOf(" ") + 1);
    }

    $("#propertyInfo, #legalDesc, #taxes, #additionalTaxes, #mortgagesLiensCourt, #additionalMatters")
     .addClass('shiftTabClass')
     .bind("keydown", function (event) {
         if (event.keyCode === $.ui.keyCode.TAB &&
         $(this).data("ui-autocomplete").menu.active) {
             event.preventDefault();
         }
     })
     .autocomplete({
         source: function (request, response) {
             // delegate back to autocomplete, but extract the last term
             var term = extractLast(request.term);
             var matcher = new RegExp("^" + $.ui.autocomplete.escapeRegex(term), "i");
             //response($.grep(availableTags, function (item) {
             response($.grep(textCodes[this.element.attr('id')], function (item) {
                 return matcher.test(item);
             }));
         },
         focus: function () {
             // prevent value inserted on focus
             return false;
         },
         minLength: 1,
         select: function (event, ui) {
             var terms = split(this.value);
             // remove the current input
             terms.pop();
             // add the selected item
             terms.push(formatItem(ui.item.value));
             // add placeholder to get the comma-and-space at the end
             terms.push("");
             this.value = terms.join("  ");
             return false;
         }
     });

修改

这样做可以得到我想要的东西,但是它添加了搜索文本,即如果我输入“ja”并单击“Java”,它不仅添加了“Java”,还添加了“jaJava”。如果我可以从$(this).val()中删除搜索词,我可能拥有我想要的内容......任何人都知道如何做到这一点?

select: function (event, ui) {
     $(this).val($(this).val() + ui.item.value);
     return false;
}

1 个答案:

答案 0 :(得分:0)

我能够通过此代码得到我想要的东西,但可能还有改进空间......

    function removeLastInstance(badtext, str) {
        var charpos = str.lastIndexOf(badtext);
        if (charpos < 0) return str;
        ptone = str.substring(0, charpos);
        pttwo = str.substring(charpos + (badtext.length));
        return (ptone + pttwo);
    }

    var sTerm;

    $("#propertyInfo, #legalDesc, #taxes, #additionalTaxes, #mortgagesLiensCourt, #additionalMatters, #abstractorComments")
     .addClass('shiftTabClass')
     .bind("keydown", function (event) {
         if (event.keyCode === $.ui.keyCode.TAB &&
         $(this).data("ui-autocomplete").menu.active) {
             event.preventDefault();
         }
     })
     .autocomplete({
         source: function (request, response) {
             // delegate back to autocomplete, but extract the last term
             var term = extractLast(request.term);
             sTerm = term;
             var matcher = new RegExp("^" + $.ui.autocomplete.escapeRegex(term), "i");
             //response($.grep(availableTags, function (item) {
             response($.grep(textCodes[this.element.attr('id')], function (item) {
                 return matcher.test(item);
             }));
         },
         focus: function () {
             // prevent value inserted on focus
             return false;
         },
         minLength: 1,
         select: function (event, ui) {
             /*
             var terms = split(this.value);
             // remove the current input
             terms.pop();
             // add the selected item
             terms.push(formatItem(ui.item.value));
             // add placeholder to get the comma-and-space at the end
             terms.push("");
             this.value = terms.join("  ");
             return false;
             */
             console.log(sTerm);
             $(this).val(removeLastInstance(sTerm, $(this).val()) + ui.item.value);
             return false;
         }
     });