简体   繁体   English

自动完成的jQuery删除'-'之后的所有内容

[英]Autocomplete Jquery remove everything after ' - '

I use a simply JQUERY Autocomplete to show possibles duplicates for certains records, like Name or address when user fill in a textfield. 我使用简单的JQUERY自动完成功能来显示某些记录的可能重复项,例如用户填写文本字段时的名称或地址。

Here code 这里的代码

<script type="text/javascript">
                $(function() {
                    var availableTags = <?php include('ajax/ajax_show.php'); ?>;
                    $("#name_value").autocomplete({
                        source: availableTags,
                        autoFocus:true
                    });
                });
            </script>

Below text filed are displayes results. 文本下方是显示结果。

Name[____________________]
 Paul - 121.456.789-00
 Robert - 122.456.789-00
 Cid - 123.456.789-00

How to remove all after - (space hyphen space) and everything after it and have in textfield only name like Paul, Robert or Cid? 如何删除所有之后的字符- (空格连字符空间)及其后的所有内容,并且在文本字段中仅具有Paul,Robert或Cid之类的名称?

If you cannot change the ajax/ajax_show.php to give you the data the way you want, one of doing that is by using RegEx and replace everything after the - with javascript. 如果您无法更改ajax/ajax_show.php来以所需的方式为您提供数据,则其中之一是使用RegEx并将-之后的所有内容替换为javascript。

$(function() {
  var availableTags = <?php include('ajax/ajax_show.php'); ?>;
  $("#name_value").autocomplete({
    source: availableTags.map(function(v) { return v.replace(/(^[^-]*) .*/, '$1') }),
    autoFocus:true
   });
 });

Basically, this solution is using the map function to iterate over all elements and create a new array with the elements "cleaned" up. 基本上,此解决方案是使用map函数迭代所有元素,并使用“清除”的元素创建一个 array The method replace will use a regular expression (^[^-]*) .* to grab everything that's before the dash. 方法replace将使用正则表达式(^[^-]*) .*来获取破折号之前的所有内容。

You can play with the regular expression if you want something more specific, another example that would work is: /^(.*) - .*$/ 如果需要更具体的信息,可以使用正则表达式,另一个可行的示例是:/ /^(.*) - .*$/

If possible, I think you'd be better off returning your data from ajax_show.php as json, like: 如果可能的话,我认为最好从ajax_show.php作为json返回数据,例如:

[
  {
   "value": "121.456.789-00",
   "label": "Paul"
  },
  {
   "value": "122.456.789-00",
   "label": "Robert"
  },
  ... 
]

Then, as shown in the jQuery UI docs here: https://jqueryui.com/autocomplete/#custom-data (under the 'Custom Data and display' tab), it will only use the name as the label but you'll still have access to the Id (the value property). 然后,如此处的jQuery UI文档所示: https : //jqueryui.com/autocomplete/#custom-data (在“自定义数据和显示”标签下),它将仅使用名称作为标签,但是您将仍然有权访问ID(值属性)。

And as added good news, the way you pass your data in (by assigning it to the 'source' property) doesn't need to change at all. 而且,作为一个好消息,传递数据的方式(通过将其分配给'source'属性)根本不需要改变。

To simply remove the numbers and show the name in an input box, you can use substr and indexOf (or you can work with regular expressions and replace if you prefer). 要简单地删除数字并在输入框中显示名称,可以使用substrindexOf (或者可以使用正则表达式并根据需要replace )。 Like this: 像这样:

$( "#name_value" ).autocomplete({
    select: function( event, ui ) {
        $('#myInputBox').text(ui.item.value.substr(0, ui.item.value.indexOf(" - ")));
    }
});    

You might want to look up these two functions here and here to see how this works. 您可能需要在此处此处查找这两个功能,以了解其工作原理。

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

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