JS脚本:
<script>
/* This function is used for multiple autocomplete input values */
$(function () {
var oldID = jQuery("#old_project_manager_id").val();
if (oldID != null) {
jQuery("#project_manager_id").val(oldID);
}
function split(val) {
return val.split(/,\s*/);
}
function extractLast(term) {
return split(term).pop();
}
$("#project_manager")
// don't navigate away from the field on tab when selecting an item
.bind("keydown", function (event) {
if (event.keyCode === $.ui.keyCode.TAB && $(this).autocomplete("instance").menu.active) {
event.preventDefault();
}
})
.autocomplete({
source: function (request, response) {
$.getJSON("<?php echo SITE_URL.'project/pmlist'?>", {
term: extractLast(request.term)
}, response);
},
search: function () {
// custom minLength
var term = extractLast(this.value);
},
focus: function () {
// prevent value inserted on focus
return false;
},
select: function (event, ui) {
var terms = split(this.value);
}
})
}
</script>
还有这个:
$(function () {
var oldID = jQuery("#old_user_id").val();
if (oldID != null) {
jQuery("#user_id").val(oldID);
}
function split(val) {
return val.split(/,\s*/);
}
function extractLast(term) {
return split(term).pop();
}
$("#user")
// don't navigate away from the field on tab when selecting an item
.bind("keydown", function (event) {
if (event.keyCode === $.ui.keyCode.TAB && $(this).autocomplete("instance").menu.active) {
event.preventDefault();
}
})
.autocomplete({
source: function (request, response) {
$.getJSON("<?php echo SITE_URL.'somecontroller/someaction'?>", {
term: extractLast(request.term)
}, response);
},
search: function () {
// custom minLength
var term = extractLast(this.value);
},
focus: function () {
// prevent value inserted on focus
return false;
},
select: function (event, ui) {
var terms = split(this.value);
// remove the current input
terms.pop();
// add the selected item
terms.push(ui.item.value);
this.value = terms.join(", ");
jQuery("#user_id").val(ui.item.emp_id);
return false;
}
});
});
我已尝试使用.length()
搜索字词,但未获取正确的结果。
当用户输入内容时,将显示已注册用户的列表。如何根据提取的记录数显示No Result Found
消息。如果没有有效用户,则会显示该消息,否则将显示用户列表。
感谢。
答案 0 :(得分:0)
我会添加这样的东西:
$("#user")
// don't navigate away from the field on tab when selecting an item
.bind("keydown", function (event) {
if (event.keyCode === $.ui.keyCode.TAB && $(this).autocomplete("instance").menu.active) {
event.preventDefault();
}
})
.autocomplete({
//Your code here
response: function(event, ui) {
if(ui.content.length == 0)
{
//do what you want if no results returned
}
}
});
阅读jQuery UI的文档有助于:http://api.jqueryui.com/autocomplete/#event-response