href="/path/to/bootstrap.css" rel="stylesheet">
Event | Description | |
---|---|---|
grepper | Filters relevant results from the source. | |
highlighter | Highlights any matching results in the list. | |
itemSelected |
|
|
lookup | Determines if source is remote or local and initializes the search. | |
matcher | Looks for a match between the query and a source item. | |
render | Renders the list of results. | |
select | Selects an item from the results list. | |
sorter | Sorts the results. |
$('#demo3').typeahead({ source: [ { id: 1, full_name: 'Toronto', first_two_letters: 'To' }, { id: 2, full_name: 'Montreal', first_two_letters: 'Mo' }, { id: 3, full_name: 'New York', first_two_letters: 'Ne' }, { id: 4, full_name: 'Buffalo', first_two_letters: 'Bu' }, { id: 5, full_name: 'Boston', first_two_letters: 'Bo' }, { id: 6, full_name: 'Columbus', first_two_letters: 'Co' }, { id: 7, full_name: 'Dallas', first_two_letters: 'Da' }, { id: 8, full_name: 'Vancouver', first_two_letters: 'Va' }, { id: 9, full_name: 'Seattle', first_two_letters: 'Se' }, { id: 10, full_name: 'Los Angeles', first_two_letters: 'Lo' } ], display: 'full_name' });
blur: function (e) {
var val = this.$element.val();
this.focused = false
if(this.uploadData(val)==""){//增加的代码
this.$element.css("border","solid red 1px")
}else{
this.$element.css("border","solid green 1px")
}
this.$element.val(this.uploadData(val))
if (!this.mousedover && this.shown) this.hide()
}
<script type="text/javascript">
var data = [
{ id: "巴厘岛", name: "巴厘岛" },
{ id: "日本", name: "日本" },
{ id: "爱尔兰", name: "爱尔兰" },
{ id: "北欧", name: "北欧" },
{ id: "希腊", name: "希腊" },
{ id: "捷克", name: "捷克" },
{ id: "意大利", name: "意大利" },
{ id: "越南", name: "越南" },
{ id: "迪拜", name: "迪拜" },
{ id: "斯洛伐克", name: "斯洛伐克" },
{ id: "柬埔寨", name: "柬埔寨" },
{ id: "马耳代夫", name: "马耳代夫" },
{ id: "德法瑞意", name: "德法瑞意" },
{ id: "北欧1", name: "北欧1" },
{ id: "北欧2", name: "北欧2" }];
$(document).ready(function() {
$('#search').typeahead({
source: data,
uploadData:function(item){
var arrayData = new Array();
$.each(data, function (n, value) {//循环比较
arrayData.push(value.name);
});
var index = $.inArray(item,arrayData);
if(index==-1){
return ""; //如果不匹配清空
}
return item; //如果匹配则显示
}
});
});
script>
$("#respondentName").typeahead({
ajax: {
url: "${ctx}/order/capBusiOrder/listLegalUnit",
timeout: 100,
displayField: "horsortName",
triggerLength: 1,
dataType: 'JSON',
method: "get",
loadingClass: "loading-circle",
preDispatch: function (query) {
return {
horsortName: query, //查询条件
limit: 5, //一次只查询5条
}
},
preProcess: function(data) { // 这个方法非常重要!
// 本插件要求处理一个javascript对象而不是一个json字符串
// 同时应当注意 !
return $.parseJSON(data); //非常重要
}
},
display:'horsortName',
val:'id',
items:5,
itemSelected:function(item, value, text) {
//alert(item);//object 对象
//alert(text); //选中的名称s
}
,
uploadData:function(item){//什么都可以通过。不许要做匹配验证则写成如下这种方案
return item;
}
});