开发者

Event handler not wired after Ajax Update, new dom element (Internet Explorer)

Problem: New Inserted Dom elements aren't been wired correctly, function deletepost not firing. This happens only on IE and for new elements only added to DOM.

$(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开发者_Python百科></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;
            }
        });
    });


Use jquery live

$(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;
            }
        });
    });


'onclick' is ancient/oldschool stuff, especially when using jquery. Try this variant instead:

$('<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);                        


Yeah , You must used jQuery live() function, the more detail example can be found here http://developerfaq.wordpress.com/2011/07/28/fire-event-on-dynamically-generated-dom-element-with-jquery/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜