How would I trim the input to a JQuery auto-complete box?

Deadly 提交于 2019-12-01 11:03:58

You can do the find yourself in the source function, instead of using the built-in function, like this:

source: function( request, response ) {
  var matcher = new RegExp($.trim(request.term).replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), "i" );
  response($.grep(resources, function(value) {
    return matcher.test( value.label || value.value || value );
  }));
}

You can try a demo here. This uses $.trim() to trim down the search term before it gets passed into $.grep() to get the leading/trailing white-space ignorant effect you want.


For your edit, you can do this, but the "No Result..." will be selectable, give it a try here:

source: function( request, response ) {
  var matcher = new RegExp($.trim(request.term).replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), "i" );
  var matches = $.grep(resources, function(value) {
    return matcher.test( value.label || value.value || value );
  });
  response(matches.length ? matches : [{ label: 'No Result Found', value: '' }]);
}

Use jQuery.trim:

jQuery.trim(yourValue);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!