2017-09-16 99 views
0

我需要的名称或编号来搜索jquery的自动完成如何使用jquery自动完成中的名称和编号进行搜索?

我想这:

HTML:

<input type="text" class="form-control" id="plugins" name="plugins" /> 

脚本:

var arraY = [{name: "xxx", phone_number: "9997515744},{name: "abc", phone_number: "9619054073"},{name: "asd", phone_number: "9323135708"}] 
$("#plugins").autocomplete({source: plugin_names}); 

从上面数组我需要搜索与姓名和电话号码,但现在只有电话号码搜索工作,姓名搜索不起作用。

任何人都可以帮助我。

+0

你能和下面 – Niladri

回答

1

自动完成需要回调充当具有下面的参数

一个request对象source,具有单term属性是指文本输入中当前的值。

A response回调,它需要一个参数:要建议给用户的数据。这些数据应根据所提供的术语进行过滤,并且可以采用上述任何一种简单本地数据格式。

退房此链接 http://api.jqueryui.com/autocomplete/#option-source

你可以尝试用下面的代码

var arraY = [{name: "xxx", phone_number: "9997515744"},{name: "abc", phone_number: "9619054073"},{name: "asd", phone_number: "9323135708"}]; 
 

 
// the typed data is in request.term 
 

 
function multipleFieldMatch(request, response) { 
 
    function isMatch(s) { 
 
     return s.toLowerCase().indexOf(request.term.toLowerCase())!==-1; 
 
    } 
 
    var i, len, obj, totalMatches = []; 
 
    len = arraY.length; 
 

 
    if (request.term==="") { 
 
     response([]); 
 
     return; 
 
    } 
 

 
    for (i = 0; i<len; i++) { 
 
     obj = arraY[i]; 
 
     if (isMatch(obj.name) || isMatch(obj.phone_number)) { 
 
      totalMatches.push(obj); 
 
     } 
 
    } 
 
    response(totalMatches); 
 
} 
 

 
$("#plugins").autocomplete({ 
 
    source: multipleFieldMatch 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type="text" class="form-control" id="plugins" name="plugins" />

+1

我的回答尝试只是一个供参考,除非'minLength'设置为0,'request.term'永远不会''“'。'minLength'的默认值是1. – Twisty

+0

另外,如果行数是import蚂蚁,你可以换出'for'循环为jQuery'$ .each()'。 – Twisty

+0

是的,你是正确的最小长度部分,我只是添加了检查作为默认 – Niladri

1

你应该尝试使用AJAX的方式,并在服务器端的搜索

$('#autocomplete').autocomplete({ 
    serviceUrl: '/autocomplete/countries', 
    onSelect: function (suggestion) { 
     alert('You selected: ' + suggestion.value + ', ' + suggestion.data); 
    } 
}); 
1

从API文档,我建议使用source作为一个回调函数。

功能:第三变型中,回调,提供最大的灵活性和可用于任何数据源连接到自动填充,包括JSONP。回调得到两个参数:

A request对象,具有单个term属性,它指向文本输入中当前的值。例如,如果用户在城市字段中输入"new yo",则自动完成项将等于"new yo"

A response回调,它需要一个参数:要建议给用户的数据。这些数据应根据所提供的术语进行过滤,并且可以采用上述任何一种简单本地数据格式。在请求期间提供自定义源回调以处理错误时,这很重要。即使遇到错误,您也必须始终致电response回拨。这可确保小部件始终具有正确的状态。

此外,源阵列需要包含一个labelvalue,它可以有更多的数据,但这两个是必须的。查看更多有关自定义数据:http://jqueryui.com/autocomplete/#custom-data

我建议是这样的:

的JavaScript

var myData = [{ 
    label: "xxx", 
    value: "9997515744" 
}, { 
    label: "abc", 
    value: "9619054073" 
}, { 
    label: "asd", 
    value: "9323135708" 
}]; 
$(function() { 
    $("#plugins").autocomplete({ 
    source: function(req, resp) { 
     var results = []; 
     $.each(myData, function(k, v) { 
     // Make a pass for names 
     if (v.label.indexOf(req.term) != -1) { 
      results.push(v); 
      return; 
     } 
     // Make a pass for phone 
     if (v.value.indexOf(req.term) != -1) { 
      results.push(v); 
      return; 
     } 
     }); 
     resp(results); 
    } 
    }); 
}); 

工作例如:https://jsfiddle.net/Twisty/urtkxo46/

+0

只有当您使用开箱即用的自动完成搜索功能时才需要标签和值,因为它会查找要显示的标签以及onselect事件中要在后面选择的值。对于自定义回调,你可以使用任何属性,只要你处理搜索 – Niladri

+0

嘿它的工作正常,但我需要在下拉列表中显示名称和电话号码,如果名称为空,需要显示未命名,我会尝试锻炼它,如果你有任何想法意味着分享。 –

+0

看看我包含的自定义数据链接。应该能够从中解决问题。否则发布一个新问题。如果这个答案对你有帮助,可以考虑将其标记为答案。 – Twisty