2013-10-01 13 views
15

Thực ra tôi đang cố gắng triển khai chức năng tự động hoàn thành cho một trường văn bản nhưng nhận được lỗi ở trên, không thể hiểu tại sao tôi nhận được lỗi này. Bạn có thể giúp tôi giải quyết lỗi này không? Để bạn tham khảo tôi đang cung cấp tất cả các mã cần thiết bên dưới:

báo cáo-sinh-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> 

Xin hãy giúp tôi ra để giải quyết lỗi này. Cảm ơn trước.

+5

Bởi vì sống() đã bị xóa khỏi jQuery? – adeneo

+0

Bạn đang sử dụng phiên bản Jquery nào? –

Trả lời

26

live() đã được gỡ bỏ từ phiên bản 1.9 và đã bị phản đối từ 1.7:

Bạn sẽ được muốn on() tại ngày

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

nơi #friends có sẵn trên DOM đã sẵn sàng. Bạn không thể liên kết on() trên phần tử được tải động.

+0

Bạn có thể không cần đối số thứ ba. Nó là tùy chọn và không được sử dụng trong trình xử lý sự kiện. – Pavlo

+0

Tại sao chúng ta cần yếu tố thứ 3? Nó không hoạt động nếu không có phần tử thứ 3. – Pepper

+0

yếu tố thứ 3 là gì? –

1

Live() đã bị xóa kể từ 1,9 từ jquery. Vui lòng sử dụng on

6

Bạn có thể sử dụng .on() thay vì .live() (bị phản đối sau khi Jquery 1,7)

$(document).on('event', 'selector', function() {}); thay thế .live().

Ví dụ:

$(document).on("click", "#elementId ", function(){ alert("Do here what you want!"); // jQuery1.7+ }); 
Các vấn đề liên quan