jQuery自动补全与getJSON搜索多输入
jQuery Autocomplete with getJSON search on multiple input
我有三个输入框
HTML:Name<input type="text" class="customer_name" name="order[contact][first_name]"/><br/>
Email<input type="text" class="customer_email" name="order[contact][email]"/><br/>
Phone<input type="text" class="customer_phone" name="order[contact][phone]"/>
我需要搜索三个ajax链接
http://localhost:8000/api/users?name_fulltext=user
http://localhost:8000/api/users?email_like=1234
http://localhost:8000/api/users?phone_like=buyer
JSON: [{"id":"-1","name":"Test User","phone":"1234567","email":"buyer@web.com"}]
我想知道的重点是如何改变json名称的路径,当我点击电子邮件或电话或名称
jQuery:$('.customer_name, .customer_email, .customer_phone').autocomplete({
source: function( request ,response){
$.getJSON("/api/users?"+ "name_fulltext=" + request.term , function (data){
response($.map(data,function(opt){
return {
label : opt.name,
value : opt.name,
}
}))
})
},
minLength: 3,
select: function(event, ui) {
$('.customer_name').data(ui.item.value);
$('.customer_name').val(ui.item.label);
$('.customer_email').data(ui.item.email_value);
$('.customer_email').val(ui.item.email_label);
$('.customer_phone').data(ui.item.phone_value);
$('.customer_phone').val(ui.item.phone_label);
name_length = $('.ui-autocomplete > li').length;
event.preventDefault();
}
});
你可以试试:
var inputClicked;
$('.customer_name, .customer_email, .customer_phone').on('click', function(){
inputClicked = $(this).attr('class');
});
$('.customer_name, .customer_email, .customer_phone').autocomplete({
source: function( request ,response){
var myUrl = "/api/users?";
if(inputClicked =="customer_name") {
myUrl = myUrl + "name_fulltext=" + request.term;
} else if(inputClicked == "customer_email"){
myUrl = myUrl + "email_like=" + request.term;
} else {
myUrl = myUrl + "phone_like=" + request.term;
}
$.getJSON(myUrl , function (data){
response($.map(data,function(opt){
return {
label : opt.name,
value : opt.name,
}
}))
})
},
minLength: 3,
select: function(event, ui) {
$('.customer_name').data(ui.item.value);
$('.customer_name').val(ui.item.label);
$('.customer_email').data(ui.item.email_value);
$('.customer_email').val(ui.item.email_label);
$('.customer_phone').data(ui.item.phone_value);
$('.customer_phone').val(ui.item.phone_label);
name_length = $('.ui-autocomplete > li').length;
event.preventDefault();
}
});
相关文章:
- 用程序搜索JQuery数据表中的文本
- 在html Select中添加搜索
- Ajax Live搜索发布到Laravel视图
- 谷歌放置API:按国家或餐馆名称搜索餐馆
- 学生搜索项目jquery/javascript
- 用于搜索的聚合物嵌套绑定
- 如何将getJson的响应保存在全局变量中
- 在javascript中搜索具有重复值的两个数组中的匹配值
- Angularjs-utils高亮过滤器在搜索时破坏应用程序
- 使用javascript搜索具有用户输入的数组
- 淘汰搜索/筛选
- 搜索api在mac上显示对话框
- AngularJS/HTML/Bootstrap元素用于动态搜索结果
- 谷歌水印未显示在自定义搜索框中
- 在javascript中搜索项目列表的性能
- 正在搜索JavaScript日期选择器滑块
- JQuery中的活动搜索栏
- 如何在剑道下拉列表中按文本和值搜索
- jQuery自动补全与getJSON搜索多输入
- Getjson和.each()函数.需要搜索/循环在第二层