2011-07-25 25 views
1

問題:新插入的Dom元素未正確連線,函數deletepost未觸發。這隻發生在IE上,並且只添加到DOM的新元素。事件處理程序在Ajax更新,新的dom元素(Internet Explorer)之後未連線

$(function(){ 
     $('#postentry').submit(function() { 
      var tyu = $('#tittle_ent').val(); 
      if(tyu.length <= 2) 
      {alert('Enter More Text!'); return false;}else 
      { 
       $.ajax({ 
        type:'post', 
        url: '/posts_in.php', 
        dataType: "json", 
        data: $("#postentry").serialize(), 
        success:function(data){ 
         var tittle = data[0]; 
         var id= data[1];       
         $('<div></div>').attr('id','post'+id).addClass('boxee').html(tittle).prependTo('#boxer');       
         $('<img src="img/page-text-delete-icon.png" name="'+id+'">').attr({id:'postchk'+id,onclick: 'deletepost(this.name);'}).appendTo('#post'+id);       
         $('#tittle_ent').val('').focus(); 

        } 
       }); 
       return false; 
      } 
     }); 
    }); 

回答

1

使用jQuery現場

$(function(){ 
     $("#boxer img").live("click", function(){ 
      deletepost($(this).attr("name")); 
     }); 

     $('#postentry').submit(function() { 
      var tyu = $('#tittle_ent').val(); 
      if(tyu.length <= 2) 
      {alert('Enter More Text!'); return false;}else 
      { 
       $.ajax({ 
        type:'post', 
        url: '/posts_in.php', 
        dataType: "json", 
        data: $("#postentry").serialize(), 
        success:function(data){ 
         var tittle = data[0]; 
         var id= data[1];       
         $('<div></div>').attr('id','post'+id).addClass('boxee').html(tittle).prependTo('#boxer');       
         $('<img src="img/page-text-delete-icon.png" name="'+id+'">').attr({id:'postchk'+id,onclick: 'deletepost(this.name);'}).appendTo('#post'+id);       
         $('#tittle_ent').val('').focus(); 

        } 
       }); 
       return false; 
      } 
     }); 
    }); 
0

的 'onClick' 是古/舊校園的東西,尤其是使用jQuery的時候。試試這個變種來代替:

$('<img src="img/page-text-delete-icon.png" name="'+id+'">') 
    .attr('id', 'postchk'+id) 
    .click(function() { deletepost(this.name); }) /// use .click() instead. 
    .appendTo('#post'+id);       
相關問題