2013-10-01 10 views
15

其实我试图实现一个文本字段的自动完成功能,但得到上述错误,无法理解为什么我得到这个错误。你能帮我解决这个错误吗?供大家参考,我提供以下所有必要的代码:

报告,学生result.tpl

<link rel="stylesheet" type="text/css" href="{$control_css_url}ui-lightness/jquery-ui-1.10.3.custom.css"> 
<link rel="stylesheet" type="text/css" href="{$control_css_url}autocomplete.css"> 
<label>Name</label> 
      <div class="form-element" id="friends"> 
       <input type="text" class="" name="user_name" id="user_name" value="{$user_name}" /> 
      </div> 

<script language="javascript" type="text/javascript"> 
$(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 = $("<span>").text(friend), 
       a = $("<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); 
      }     
     });      
    }); 

</script> 

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

+5

由于Live()已经从jQuery的删除? – adeneo

+0

你正在使用哪个jquery版本? –

回答

26

live()自1.9版本已被删除,因为1.7被弃用:

你会想现在天on()

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

其中#friends可以用DOM准备。您无法在动态加载的元素上绑定on()

+0

你可能不需要第三个参数。它是可选的,不用于事件处理程序。 – Pavlo

+0

为什么我们需要第三个元素?没有第三个元素,它就无法工作。 – Pepper

+0

第三个元素是什么? –

1

Live()自1.9以来已从jquery中删除。请使用on

6

可以使用的.on()代替.live()(jQuery的1.7后不推荐使用)

$(document).on('event', 'selector', function() {});取代.live()

如:

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