简体   繁体   English

我如何将输入修剪到JQuery自动完成框?

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

Is there any way to trim (remove leading/trailing spaces) the input entered by a user into a jQuery auto-completing text <input> box before it is matched against the list of names:values? 有没有办法在将用户<input>的名称与值列表匹配之前将其<input>到jQuery自动完成文本<input>框中的内容修剪掉(删除前导/后空格)? I currently have a textbox in which users are meant to enter names. 我目前有一个文本框,用户可以在其中输入名称。 The names are then matched against a list of name:value pairs by jQuery: 然后通过jQuery将名称与name:value对的列表匹配:

<script type="text/javascript">

var resources = [
               <?php 
                    foreach($data['Resource'] as &$row){
                        $Name = $row['Forename']." ".$row['Surname'];  
                        echo "{";
                        echo "  label:'$Name',";
                        echo "  value:'$row[EmployeeNumber]'";
                        echo "},";
                    }
                 ?>
                ];

    jQuery(function(){
        jQuery('#Resource').autocomplete({
            source: resources,
            focus: function(event, ui) {
                jQuery('#Resource').val(ui.item.label);
                return false;
            },          
            select: function(event, ui) {
                jQuery('#Resource').val(ui.item.label);
                jQuery('#EmployeeNumber').val(ui.item.value);
                return false;
            }
        });
    }); 
</script>

My problem is that if the user enters a name that matches one in the resources map, but with spaces after it, it won't be matched and thus no value will be assigned to the input. 我的问题是,如果用户输入的名称与resources映射中的名称匹配,但名称后面带有空格,则该名称将不匹配,因此不会为输入分配任何值。 I would like for trailing spaces at least (if not also leading spaces) to be ignored on this mapping if possible. 我希望在此映射中至少忽略尾随空格(如果不是前导空格)。

Additionally, would it be possible to add a default value for the input box if no map is found? 另外,如果找不到地图,是否可以为输入框添加默认值?

EDIT: 编辑:

As an aside, is it possible to have a no-match entry shown in the drop-down autocomplete box if the user types something that doesn't match? 顺便说一句,如果用户键入不匹配的内容,是否可能在下拉自动完成框中显示不匹配项? Apologies for the after-question edit. 对问题后的编辑表示歉意。

You can do the find yourself in the source function, instead of using the built-in function , like this: 您可以在source函数中查找自己,而不是使用内置函数 ,如下所示:

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. 它使用$.trim()修剪搜索项,然后再将其传递到$.grep()以获得所需的前导/后缀空白效果。


For your edit, you can do this, but the "No Result..." will be selectable, give it a try here : 您可以进行编辑,但是可以选择“ No Result ...”,请在此处进行尝试

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: '' }]);
}

使用jQuery.trim

jQuery.trim(yourValue);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM