2016-11-05 19 views
0

$.ajax({中,我用標籤返回標籤,但我想在第一個標籤下面添加第二個標籤,我試過+ "\n" +。更好的問題是我如何在那裏插入html?因爲以後也許我想插入表或其他東西來好看。Ajax源代碼返回多個標籤。如何在第一個標籤後添加新行

下面是腳本:

<script type="text/javascript"> 
$(document).ready(function() { 
    function split(val) { 
     return val.split(/,\s*/); 
    } 
    function extractLast(term) { 
     return split(term).pop(); 
    } 

    $("#search").autocomplete({ 
     source: function (request, response) { 
      $.ajax({ 
       url: "/Jobs/GetTags", 
       type: "POST", 
       dataType: "json", 
       data: { term: extractLast(request.term)}, 
       success: function (data) { 
        response($.map(data, function (item) { 
         return {        
          label: item.Name + "\n" + item.Description, 
          value: item.Name,        
         }; 
        })) 
       } 
      }) 
     }, 

     focus: function() { 
      return false; 
     }, 

     search: function() { 
      // custom minLength 
      var term = extractLast(this.value); 
      if (term.length < 2) { 
       return false; 
      } 
     }, 

     select: function (event, ui) { 
      var terms = split(this.value); 
      // remove the current input 
      terms.pop(); 
      // add the selected item 
      terms.push(ui.item.value); 
      // add placeholder to get the comma-and-space at the end 
      terms.push(""); 
      this.value = terms.join(", "); 
      return false; 
     }, 

     messages: { 
      noResults: '', 
      results: function() { } 
     } 
    }); 
}) 

這裏是剃刀:

@using (Html.BeginForm()) 
{ 
    @Html.AntiForgeryToken() 
    <p> 
     Tags @Html.TextBox("search")  
    </p> 
} 

回答

0

找到了!

我在末尾添加以下內容:

}).data("ui-autocomplete")._renderItem = function (li, item) { 
     return $("<li></li>") 
      .data("item.autocomplete", item) 
      .append("<a>" + item.label1 + "</a>" + "<br />" + item.label2 + "</a>").before("<br />") 
      .appendTo(li); 
    }; 
相關問題