0

这是我的 jQuery

$('.vote_down').live('click', function() {
    var $votes = $(this);
    var c_id = $(this).closest('.c_id').val();
    var c_vote = $(this).closest('.c_vote').val();
    $.ajax({
        type: "POST",
        url: "votes.php",
        data: "c_id="+c_id+"&c_vote="+c_vote,
        success: function(html){
            $votes.parent().html(html);             
        }
    });
});

这是它从中提取的html:

varsc_id目前c_vote一无所获

<div class="votes">
    <input type="hidden" class="c_id" value="5" />
    <input type="hidden" class="c_vote" value="2" />
    <img src="down_vote.png" border="0" class="vote_down" alt="Down Vote" />
</div>
4

2 回答 2

4

您使用了错误的功能。closest获得最近的祖先。输入字段不是图像的祖先,它们是兄弟姐妹

你可以做:

var c_id = $(this).prevAll('.c_id').val();
var c_vote = $(this).prevAll('.c_vote').val();

或者如果顺序始终相同:

var c_id = $(this).prev().prev().val();
var c_vote = $(this).prev().val();

参考prevAllprev

于 2011-03-16T23:30:16.363 回答
0
$('.vote_down').live('click', function() {
    var $votes = $(this);
    var c_id = $(this).prev('input.c_id:first').val();
    var c_vote = $(this).prev('input.c_vote:first').val();
    $.ajax({
        type: "POST",
        url: "votes.php",
        data: "c_id="+c_id+"&c_vote="+c_vote,
        success: function(html){
            $votes.parent().html(html);             
        }
    });
});

-在您的评论后编辑,我敢打赌,我认为 prev()- 如果您总是使用这种 HTML 结构,为什么不使用prev();? 试试上面的代码

于 2011-03-16T23:25:58.667 回答