Skip to content

Instantly share code, notes, and snippets.

@wingwan
Forked from cfjedimaster/gist:2929966
Created June 19, 2013 12:21
Show Gist options
  • Save wingwan/5813874 to your computer and use it in GitHub Desktop.
Save wingwan/5813874 to your computer and use it in GitHub Desktop.
<!doctype html>
<html>
<head>
<title>Example 1</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("#search").on("input", function(e) {
var val = $(this).val();
if(val === "") return;
//You could use this to limit results
//if(val.length < 3) return;
console.log(val);
$.ajax({ type : "get",
async:false,
url : "http://suggestion.baidu.com/su?wd=" + val,
dataType : "jsonp",
jsonp: "cb",
success : function(json){
var dataList = $("#searchresults");
dataList.empty();
if(json.s && json.s.length) {
for(var i=0, len=json.s.length; i<len; i++) {
var opt = $("<option></option>").attr("value", json.s[i]);
dataList.append(opt);
}
}
},
error:function(){
alert('fail');
}
});
});
})
</script>
</head>
<body>
<p>
<input type="text" name="search" id="search" placeholder="Type Something" list="searchresults" autocomplete="off">
<datalist id="searchresults"></datalist>
</p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment