开发者

jQuery parent usage

First time using jQuery parent and got confused, here's my html:

<div id="friends_inveni" class="friends_rec" style="">
    <br>
    <div style="height: 280px; overflow-y: auto;">
        <div style="width:150px;float:left;font-size:11px;color:White;">
            <input type="checkbox" name="friends" value="3265" id="friend_3265">
            <img style="width:24px;vertical-align:middle;" src="https://graph.facebook.com/661382259/picture">
            Test1
        </div>
        <div style="width:150px;float:left;font-size:11px;color:White;">
            <input type="checkbox" name="friends" value="3265" id="friend_3265">
            <img style="width:24px;vertical-align:middle;" src="https://graph.facebook.com/599567764/picture">
            Test2
        </div>
    </div>
    <br clear="all">
    <div class="action_new_comment">
        <textarea class="action_comment_input" onkeyup="check_height(this);" style="height:36px;"></textarea>
        <开发者_StackOverflow中文版br clear="all">
        <a class="action_btn" style="font-size:11px;" name="comment" id="A1">Send Recommendation</a>
    </div>
</div>

Here's my jQuery code:

jQuery(document).ready(function() {
        $(".action_btn").live('click', function() {
            var friends = new Array();
            $(this).parents('.recommend').find('input:checked').each(function(k) { friends[k] = $(this).val(); });
            var comment = $(this).parents('.recommend').find('textarea').val();

            if (friends.length == 0) {
                alert('Please select at least one friend.');
            } else {
                $.post("update.php",
                    { uid: <?php echo $_SESSION['uid']; ?>, mid: <?php echo $_GET['mid']; ?>, friends: friends, comment: comment },
                    function() {
                    var newComment = $('<div  class="bubble_confirmation"><h1>Recommendation Sent!</h1><br /><br > <a href="#" onclick="$(this).parent(\'div\').remove();return false;">Send more</a></div>');
                        $(this).parents('.recommend').append(newComment.fadeIn());
                        $(this).parents('.recommend').find("input:checkbox:checked").attr("checked", "");
                        $(this).parents('.recommend').find('.action_comment_input').val('');
                    });
            }
        });
    });

Issue is that: 1. I can't get the content of the textbox 2. I can't get the array of userid

Why is this?


It looks as though your HTML class friends_rec should be recommend, judging from your jQuery code.

Also, since we don't know what $(el) is, that should probably be $(this), instead.


Addition to what Herman said you should do:

var $parent =  $(this).parents('.recommend:first')

inside of your ajax callback. The :first prevents jQuery from going up the whole tree to look for more elements of that classname after you've already found the element you're looking for and keeping it as a variable reference allows you to not create a new jQuery object each time you need to use it.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜