Created
October 9, 2013 05:34
-
-
Save bachue/6896673 to your computer and use it in GitHub Desktop.
an example about dynamic datalist, work for chrome & firefox
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>Ruby on Rails: Welcome aboard</title> | |
<script src="http://code.jquery.com/jquery-1.10.2.js"></script> | |
<script> | |
$(function() { | |
$('#input').on('input', function(e) { | |
var val = $(this).val(); | |
if(val === "") return; | |
$.get('/query', {text: val}, function(res) { | |
var datalist = $('#datalist'); | |
datalist.empty(); | |
res = JSON.parse(res); | |
$.each(res, function (index, ele) { | |
var opt = $('<option></option>').attr('value', ele); | |
datalist.append(opt); | |
}); | |
}); | |
}); | |
}); | |
</script> | |
</head> | |
<body> | |
<div> | |
<input type="text" id="input" list="datalist"/> | |
<datalist id="datalist"></datalist> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment