将单击的元素id发送到ajax函数

时间:2012-11-21 08:57:53

标签: jquery ajax function variables

将变量发送到我的ajax调用时遇到问题,这样做的目的是当我点击链接时我有一个将html设置为变量的函数,例如ID

像这样

elements.navitem.click(function (event, settings, elements, id) {
    event.preventDefault();

    id = $(this).html();
    console.log(id);
    ajax(id);
});

单击时可以触发ajax功能。

function ajax(elements, settings, id) {

elements = $.extend({}, defaultelements, elements || {});
settings = $.extend({}, defaultsettings, settings || {});

console.log('Requested id is ' + id);

$.ajax({
    url: location,
    data: {
        json: 'get_page',
        slug: id
    },
    success: function(data) {
        var current = $('.subpage');

        console.log('getpage ' + id);

        if( settings.url == 'work' ) {
            close();
            settings.motion = false;
        } else if( current.attr('id') == settings.url ) {
            settings.motion = false;
            return;
        } else {
            var html = '<article id="' + data.page.slug + '" data-info="' + data.page.slug + '" class="subpage"></article>';

            if(settings.motion) return false;
                else settings.motion = true;

            elements.thumbs.before(html);
            var container = $('.subpage');
            elements.body.prepend('<div class="spinner"></div>');
            $('.spinner').hide();
            container.load(data.page.url).hide();

            elements.thumbs.stop().animate({
                'top' : '100%'
            }, settings.speed, settings.easing, function() {
                $('.spinner').fadeIn(259);
                setTimeout(function() {
                    $('.spinner').fadeOut(559);
                    setTimeout(function() {
                        container.fadeIn(659);
                    }, 450)
                    setTimeout(function() {
                        $('.spinner').remove();
                    }, 200)
                }, 1250);
                settings.motion = false;
                movePage.prototype.pagein();
                $('article:not(#' + data.page.slug + ')').remove();
                location.hash = '#!/' + settings.url;
            });
        }
    }
});
}

但它返回undefined,我无法弄清楚原因! sadface

如果有帮助,我会在所有内容之前设置“id”

任何人都有任何想法或意见指出我正确的方向? 先进的Thnx。

0 个答案:

没有答案