jQuery多个自动补全,包含来自url的json数据

时间:2019-01-30 10:51:53

标签: jquery json jquery-ui-autocomplete

我正在尝试使用来自url的json数据使jQuery在输入字段上自动完成。

目前只是

    $(function() {
    $("#new-user").autocomplete({
    minLength: 0,

    source: "/autocomplete/"

    focus: function() {return false;},

    select: function( event, ui ) {
            var terms = split( this.value );
            terms.pop();
            terms.push( ui.item.value );
            terms.push( "" );
            this.value = terms.join( ", " );
            return false;
        }}

}

它适用于第一个输入,我需要用逗号分隔多个输入。我从jQuery网站上找到了用于多个输入的课程代码,但是我不了解如何在我的情况下应用extractLast()函数。我该怎么办?

来自jQuery网站的代码:

    function split( val ) {
  return val.split( /,\s*/ );
}
function extractLast( term ) {
  return split( term ).pop();
}

$( "#tags" )
  // don't navigate away from the field on tab when selecting an item
  .on( "keydown", function( event ) {
    if ( event.keyCode === $.ui.keyCode.TAB &&
        $( this ).autocomplete( "instance" ).menu.active ) {
      event.preventDefault();
    }
  })
  .autocomplete({
    minLength: 0,
    source: function( request, response ) {
      // delegate back to autocomplete, but extract the last term
      response( $.ui.autocomplete.filter(
        availableTags, extractLast( request.term ) ) );
    },
    focus: function() {
      // prevent value inserted on focus
      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;
    }
  });

0 个答案:

没有答案