jQuery没有从按钮获取动态更新的数据 [英] jquery isn't getting dynamically updated data from button

查看:92
本文介绍了jQuery没有从按钮获取动态更新的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个按钮,该按钮会更新其自身的值,并且再次单击时,jQuery不会选择更新后的值.

I've created a button, which updates the values of itself, and when clicked on (again), jquery isn't picking up the updated values..

在第一次单击之前:

<a href="news" data-start="2" data-limit="2" data-page="2" class="btn load-more">Load more</a>

jQuery:

$(document).on('click','.load-more',function(e){
        e.preventDefault();

        var start   = $(this).data('start'),
            limit   = $(this).data('limit'),
            page    = $(this).data('page'),
            type    = $(this).attr('href');

        alert( start+' '+limit+' '+page );

        $.ajax({
            url:'/index.php?route=news/news/getnext&start=4&limit=4&page=2',
            method:'GET',
            data:{start:start,limit:limit,page:page},
            dataType:'json',
            success:function(data){

                .... doing stuff ....

                if(data.pagination === true){
                    $('.load-more').attr('data-start',data.start).attr('data-limit',data.limit).attr('data-page',data.next);
                } else {
                    $('.load-more').fadeOut(250,function(){
                        $(this).remove();
                    });
                }

            }
        })(data);
    });

这将按预期发出"2 2 2"警报.

This alerts "2 2 2" as expected.

更新后的按钮(第一次单击后):

The updated button (after the first click):

<a href="news" data-start="4" data-limit="2" data-page="3" class="btn load-more">Load more</a>

然后,我再次单击该按钮,它仍然提示按钮已更新为"2 2 2",而不是"4 2 3".

Then I click the button again, and it still alerts "2 2 2", instead of "4 2 3", which the button was updated to..

对此有何想法?我似乎无法正常工作,我已经尝试了基本的click函数并进行了委派,但是它没有按预期工作.

Any ideas on this? I can't seem to get it working, I've tried the basic click function and also delegate but it's not working as expected.

推荐答案

更新以下代码后:

 <a href="news" data-start="4" data-limit="2" data-page="3" class="btn load-more">Load more</a>

    $(document).on('click','.load-more',function(e){
    e.preventDefault();

    var start   = $(this).attr('data-start');
        limit   = $(this).attr('data-limit');
        page    = $(this).attr('data-page');


    alert( start+' '+limit+' '+page );
});

这篇关于jQuery没有从按钮获取动态更新的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆