-
-
Save wingwan/5813874 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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