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(){...})
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongzhen7108 2013-02-13 21:44

    Try removing the $() from form.

    You have this:

    var form = $(this);
        var div_add_comment = $(form).parent();
        var div_comments = $(div_add_comment).parent();
    

    It should be this:

    var form = $(this),
        div_add_comment = form.parent(),
        div_comments = $(div_add_comment).parent();
    

    Since you declared var form = $(this); you don't need to wrap form...how you have it now is the equivalent of $((form))

    Not sure if this will fix your problem, but jQuery may be getting hung up on this since you are spawning children from $(this).

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题