2013-03-23 90 views
0

我正在使用多個值的jQuery UI示例中的代碼。當我嘗試插入換行符時,它會讓我,但是當我嘗試添加新項目時,它將刪除換行符。我怎樣才能保留換行符?自動完成多個值,保留LineBreak

Ref。 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」,但它增加了「jaJava」。如果我可以從$(this).val()中刪除搜索詞,我可能會有我想要的...任何人都知道如何做到這一點?

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

你說的意思是「把一個換行符嗎?」 – 2013-03-23 15:21:14

+0

我有一個我提供自動完成的textarea。我希望用戶能夠輸入換行符(通過按「Enter」鍵)。他們可以做到這一點,但是當他們去添加一個新的自動完成項目時,它會刪除換行符。我的編輯讓我更接近,我想我幾乎在那裏...... – user1477388 2013-03-23 15:25:07

回答

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; 
     } 
    });