在jquery中使用兄弟姐妹的正确方法?

时间:2013-09-21 08:20:07

标签: javascript jquery

我有一个从数据库返回更新值的ajax函数。它是在某个按钮的点击事件下实现的。

DIV:

<div class="rep">
  <div class="up_arrow"></div>
  <div class="rep_count"></div>
</div>

页面上有大约10个相同的div重复。 up_arrow是用户点击的位置。

我正在尝试更新rep_count中的数据,并将up_arrow的类更改为up_arrowed

Ajax的成功功能:

success: function(data){
  $(this).addClass('.up_arrow').removeClass('.up_arrowed');
  $(this).css('background-position','0 40px');
  $(this).next('.rep_count').html(data);
}

这个成功函数是ajax函数的一部分,它是在click函数上调用的,这就是我使用this来引用它的兄弟姐妹的原因。这不是我所期待的。我尝试使用siblings而不是next,但这也没有用魔法。

提前感谢您的帮助。

编辑: 点击功能:

$('.up_arrow').each(function() {
    $(this).click(function(event) {

        var resid = $(this).attr('name');

        var post_data = {
            'resid' : resid,
            '<?php echo $this->security->get_csrf_token_name(); ?>' : '<?php echo $this->security->get_csrf_hash(); ?>'
        };

        if(resid){
            $.ajax({
                type: 'POST',
                url: "/ci_theyaw/restaurants/plusrepo",
                data: post_data,
                success: function(data){
                    //console.log($(this).siblings('.rep_count').text());
                    $(this).addClass('.up_arrow').removeClass('.up_arrowed');
                    //$(this).css('background-position','0 40px');
                    //$(this).next('.rep_count').html(data);
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    alert(xhr.status);
                    console.log(xhr.responseText);
                    alert(thrownError);
                }
            });
        }
    });
});

4 个答案:

答案 0 :(得分:1)

成功处理程序内的

this不会引用与在ajax调用之外引用的相同对象。

一种解决方案是使用一个闭包变量self,它引用被点击的元素,然后在成功处理程序中使用它。还有其他一些变化

//there is no need to use .each() here
$('.up_arrow').click(function () {
    var resid = $(this).attr('name');
    var post_data = {
        'resid': resid,
            '<?php echo $this->security->get_csrf_token_name(); ?>': '<?php echo $this->security->get_csrf_hash(); ?>'
    };

    var self = this;
    if (resid) {
        $.ajax({
            type: 'POST',
            url: "/ci_theyaw/restaurants/plusrepo",
            data: post_data,
            success: function (data) {
                //console.log($(this).siblings('.rep_count').text());

                //you had swapped the class names here also should not use . in front of the class name, it is used only for class selector
                $(self).addClass('up_arrowed').removeClass('up_arrow');
                //$(this).css('background-position','0 40px');
                //$(this).next('.rep_count').html(data);
            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert(xhr.status);
                console.log(xhr.responseText);
                alert(thrownError);
            }
        });
    }
});

另一个解决方案是使用$ .proxy,如下所示传递自定义上下文

success: $.proxy(function(data){
  $(this).addClass('.up_arrow').removeClass('.up_arrowed');
  $(this).css('background-position','0 40px');
  $(this).next('.rep_count').html(data);
}, this)

答案 1 :(得分:1)

您需要将用户点击的项目传递给成功函数。我会将您的代码更改为:

$('.up_arrow').click(function() {
    //you don't need an .each() loop to bind the events    

    var TheClickedItem = $(this);
    .......

    success: function(data){ 
       //now you're accessing/modifying the item that was actually clicked.
       TheClickedItem.addClass('.up_arrow').removeClass('.up_arrowed');
       TheClickedItem.....
    }

答案 2 :(得分:1)

问题是this没有引用成功回调中的被点击元素。

使用 $.ajax 中的 context 选项在成功回调中指定您需要的this

$.ajax({
    ...
    context: $(this), // now the clicked element will be `this` inside the callback
    ...
    success: function(data) {
        // here 'this' refers to the clicked element now
    },
    ...
});

答案 3 :(得分:1)

您需要在onclick上下文中保存,因为在ajax-success函数指的是其他上下文。你应该这样做:

$('.up_arrow').on('click', function() {
    var self = this; // save this refering to <a>
    $.ajax(.....,
        success: function() {
           // this - refers to success-function context, but self - refers to 'a'-onclick handler
            $(self).addClass('.up_arrow').removeClass('.up_arrowed');
            $(self).css('background-position','0 40px');
            $(self).next('.rep_count').html(data);     
        }
    )
})