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

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

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

我的理解是,如果你有项目苹果,芒果,香蕉,(空白数组项)你将留下这个字符串“苹果,芒果,香蕉,”删除最后一个逗号,你将一个简单的子串删除最后一个字符

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