bootstrap3-typeahead自动补全

<li>
<p>xxx:</p>
<input id="caseId" value="" name="caseId" hidden>
<input id="caseName" list="caseList" onchange="echoId2()" autocomplete="off"/>
<datalist id="caseList"></datalist>
</li>

<script type="text/javascript" src="../ajax/libs/typeahead/bootstrap3-typeahead.min.js"></script>//要引入js


<script>
window.onload=search();
window.onload=showSearch();
function search(){
//热搜(输入一个数字就查询)
$('#caseName').typeahead({
source: function (query, process) {
$.ajax({
type: "Post",
url: ctx+"case/file/search?caseName=" + query ,
contentType: "application/json;charset=utf-8",
dataType: "json",
timeout: 50000,
async: false,
success: function (data) {
var datas = data.data;
if (datas == query) {
return;
}
// process(datas);
},
error: function (err) {
console.log(err);
alert(err);
}
});
}

});
}

//datalist的数据赋值
function showSearch(){
$.ajax({
type: "Post",
url:ctx+"case/file/search?caseName="+$("#caseName").val(),
contentType: "application/json;charset=utf-8",
dataType: "json",
timeout: 50000,
async: false,
success: function (data) {
if(data.data!=null){
var datas = data.data;
var caseList = '';
for (var a=0;a<data.data.length;a++){
caseList+='<option value='+data.data[a]+'>'+data.data[a]+'</option>';
}
$("#caseList").html(caseList);
$("#caseName").load();
}
},
error: function (err) {
console.log(err);
alert(err);
}
});
}


function echoId2() {
$.ajax({
url:ctx+"case/file/echoid?caseName="+$("#caseName").val(),
type:"post",
async:false,
success:function (data) {
var oneData = data.data;
$("#caseId").val(oneData.id);
}
})
}

</script>
posted @ 2021-01-12 13:39  我没有出家  阅读(149)  评论(0编辑  收藏  举报