为什么我在以下场景中收到jQuery错误“TypeError:$(…)。live不是函数”?

实际上我正在尝试将自动完成function实现到一个文本字段,但得到上述错误,无法理解为什么我收到此错误。 你能帮我解决这个错误吗? 供您参考我在下面提供所有必要的代码:

报告-学生result.tpl

   
$(function(){ var class_id = $('#class_id').val(); var section_id = $('#section_id').val(); //attach autocomplete $("#user_name").autocomplete({ //define callback to format results source: function(req, add){ //pass request to server $.getJSON("report_student_result.php?callback=?op=get_student_names&class_id="+class_id+"&section_id="+section_id, req, function(data) { //create array for response objects var suggestions = []; //process response $.each(data, function(i, val){ suggestions.push(val.name); }); //pass array to callback add(suggestions); }); }, //define select handler select: function(e, ui) { //create formatted friend var friend = ui.item.value, span = $("").text(friend), a = $("").addClass("remove").attr({ href: "javascript:", title: "Remove " + friend }).text("x").appendTo(span); //add friend to friend div span.insertBefore("#user_name"); }, //define select handler change: function() { //prevent 'to' field being updated and correct position $("#user_name").val("").css("top", 2); } }); //add click handler to friends div $("#friends").click(function(){ //focus 'to' field $("#user_name").focus(); }); //add live handler for clicks on remove links $(".remove", document.getElementById("friends")).live("click", function(){ //remove current friend $(this).parent().remove(); //correct 'to' field position if($("#friends span").length === 0) { $("#user_name").css("top", 0); } }); });

请帮我解决这个错误。 提前致谢。

live()从版本1.9开始被删除,自1.7以来被弃用:

你现在想要on()

 $('#friends').on("click", ".remove", document.getElementById("friends"), function(){ 

哪里有#friends可用于DOM准备就绪。 您不能on()动态加载的元素上绑定on()

您可以使用.on()而不是.live() (在Jquery 1.7之后弃用)

$(document).on('event', 'selector', function() {}); 替换.live()

例如:

 $( document ).on( "click", "#elementId ", function(){ alert( "Do here what you want!" ); // jQuery1.7+ }); 

Live()已从jquery中删除了1.9。 请使用