删除逗号到最后的值(自动完成多个选择)

时间:2014-03-13 04:25:18

标签: jquery

我有这段代码,我只想删除用户将插入的最后一个值的逗号。 如果我要单击“添加”按钮,将删除最后一个逗号。 代码:

$(function() {
    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
      .bind( "keydown", function( event ) {
        if ( event.keyCode === $.ui.keyCode.TAB &&
            $( this ).data( "ui-autocomplete" ).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;
        }
      });
  });

1 个答案:

答案 0 :(得分:0)

我的理解是,如果你有项目苹果,芒果,香蕉,(空白数组项目) 你会留下这个字符串" apple,mango,banana," 要删除最后一个逗号,您需要一个简单的子字符串来删除最后一个字符

val = val.substring(0,val.length -1)