jquery json遍历和动态绑定事件

时间:2022-09-06 23:36:56
<div id='tmpselectorList' style='border: 1px solid grey;max-height: 150px;position:absolute;text-align: left; overflow: auto;background:white;width:153px;'>

                </div>
<script type="text/javascript">
$(document).ready(function () {
$("#engineerName").change(function () {
//ajax获取数据库查询得到的数据
var name = $("#engineerName").val();
if(name != ""){
$.ajax({
type: "get",
dataType: "json",
url:"${contextPath}/carUpload/enginNameByLike/" + name +"?random="+Math.random(),
success: function (result) {
//清空原来的旧数据
$("#tmpselectorList").empty(); var html = "<ul class='ui-menu' style='cursor: pointer;'>"; //json数据遍历
$.each( result.data, function(index,item){
//alert("index:"+index+", item:"+ item);
html += "<li>"+item+"</li>";
}); html += "</ul>";
$("#tmpselectorList").append(html); }
});
}
}); //jquery动态绑定事件
$("#tmpselectorList").on("click","ul li",function(){
//赋予值
$("#engineerName").val( $(this).text() );
}) ;
});
</script>