dqtl46964 2013-02-13 21:09
浏览 35
已采纳

成功的ajax函数后,其他jquery函数无法正常工作

EDIT

JQUERY-AJAX REQUEST CODE:

<script type="text/javascript">
$(document).ready(function(){
$(".form").submit( function(e) {
    e.preventDefault();
    var form = $(this);
    var div_add_comment = $(form).parent();
    var div_comments = $(div_add_comment).parent();
    $.ajax({
        type: "POST",
        data: $(form).serialize(),
        url: "includes/comment.php",
        success: function(msg){
            $(div_comments).html(msg);
        }
    });
    return false;
});
});
</script>

JQUERY SHOW ALL - COLLAPSE COMMENTS CODE

<script type="text/javascript">
$(document).ready(function(){
$('.see_all').click(function(){
    var thisItem = $(this);
    thisItem.parent().find('#comment2').slideDown('fast');
    thisItem.parent().find('.collapse').css('display','inline-block');
    thisItem.css('display','none');
    return false;
}); 

$('.collapse').click(function(){
     var thisItem = $(this);
     thisItem.parent().find('#comment2').slideUp('fast');
     thisItem.css('display','none');
     thisItem.parent().find('.see_all').css('display','inline-block');
     return false;
})
})
</script>

JQUERY REMOVE DEFAULT VALUE TEXT UPON FOCUS - TEXTAREA

<script type="text/javascript">
$(document).ready(function(){
var Input = $('textarea[name=comment]');
var default_value = Input.val();

$(Input).focus(function() {
    if($(this).val() == default_value)
    {
         $(this).val("");
    }
}).blur(function(){
    if($(this).val().length == 0)
    {
        $(this).val(default_value);
    }
});
})
</script>

Please let me know if you need anything else, I have the damndest of time copying code and formatting it in these posts.

END EDIT

I am having a weird little problem. I have created a jquery-ajax function to transfer data from a comments section of my page. The page has an instance of this form under each user post. So this page will have X amount of posts with X amount of comments for each posts, like a social network. My ajax request sends, recieves and displays the data perfectly BUT I have two other jquery functions called on elements inside that no longer work after the ajax function returns the html. All the other ones not acted upon by the ajax function STILL WORK. I have the checked and rechecked the response html from the ajax function and it is identical to the html of a standard post-comment instance.

Please let me know what you would like to see or if you have questions.

Thanks, your help is always appreciated!

  • 写回答

2条回答 默认 最新

  • dongpixi2648 2013-02-13 21:13
    关注

    Be sure to bind the jQuery functions in such a way that the element doesn't have to exist.

    $('ul').on('click', 'li', function(){ /* do something */ });
    

    This will execute on LIs that have been added after the binding of the function.

    In your case you would want to bind to the parent of the comments section and target the elements that have the click behavior.

    $('.comments')
        .on('click', '.see_all', function(){...})
        .on('click', '.collapse', function(){...})
        .on('focus', 'textarea[name=comment]', function(){...})
        .on('blur', 'textarea[name=comment]', function(){...})
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突