将逗号删除为最后一个值(自动完成多项选择) [英] Remove comma to the last values (autocomplete multiple select)

查看:115
本文介绍了将逗号删除为最后一个值(自动完成多项选择)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have this code and I just want to remove comma to the last value that the user will insert. If I am going to click the add button the comma to the last will be remove. Code:

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

推荐答案

我的理解是,如果您有苹果,芒果,香蕉(空白数组项目)项 您将得到该字符串"apple,mango,banana" 删除最后一个逗号,您将使用一个简单的子字符串删除最后一个字符

my understanding is that if you had items apple, mango, banana, (blank array item) You would be left with this string "apple,mango,banana," to remove the last comma you would to a simple substring to remove last character

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

这篇关于将逗号删除为最后一个值(自动完成多项选择)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆