使用jQuery $ .ajax()的问题

时间:2011-03-16 00:09:23

标签: jquery ajax

我在使用ajax和jQuery时遇到问题...

我有以下jscript:

$(document).ready(function() {

    $('rel[close]').click(function() {
        $(this.parent).close();
    });

    $('a[rel=track]').click(function() {
        var track = this.attr('href');
        track = track.replace(/^.*#/, ''); // remove the hash part of tag id
        var data = 'track=' + track + '&user=<? echo $user; ?>',
        $.ajax({
            url: 'purchase.php',
            type: 'GET',
            data: data,
            cache: false,
            success: function (html) {
                $('#purchasePanel').html(html);
                $('#purchasePanel').show();
                }
            }
        )
    });
});

调试器说他们的行$.ajax({出现问题意外令牌.

这是CSS:

#purchasePanel { position:absolute; width:200px; height:115px; z-index:1; visibility:hidden; }

使用此HTML:

<div id="purchasePanel"> <a href="Close" rel="close">Close this window</a> </div>

脚本无法运行,也没有弹出隐藏的DIV或其他任何内容。

任何想法为什么?

非常感谢, 亚历

4 个答案:

答案 0 :(得分:3)

你有一些不正确的大括号。(它们只是以一种非常令人困惑的方式放置而且没有正确缩进)

$.ajax来电之前,您有,而不是;

您还需要使用$(this).attr('href');,因为this是一个普通的DOM对象而.attr()需要一个jQuery对象。

以下是应该运行的代码:

$(document).ready(function() {

    $('rel[close]').click(function() {
        $(this.parent).close();
    });

    $('a[rel=track]').click(function() {
        var track = $(this).attr('href');
        track = track.replace(/^.*#/, ''); // remove the hash part of tag id
        var data = 'track=' + track + '&user=<? echo $user; ?>';
        $.ajax({
            url: 'purchase.php',
            type: 'GET',
            data: data,
            cache: false,
            success: function (html) {
                $('#purchasepanel').html(html);
                $('#purchasepanel').show();
            }
        });
    });
});

答案 1 :(得分:1)

这可能是逗号而不是分号:

$('a[rel=track]').click(function() {
    var track = this.attr('href');
    track = track.replace(/^.*#/, ''); // remove the hash part of tag id
    var data = 'track=' + track + '&user=<? echo $user; ?>', <--------------
    $.ajax({
        url: 'purchase.php',
        type: 'GET',
        data: data,
        cache: false,
        success: function (html) {
            $('#purchasePanel').html(html);
            $('#purchasePanel').show();
            }
        }
    )
});

答案 2 :(得分:0)

我不知道这是否会导致问题,但是在ajax函数调用结束时你没有分号。

答案 3 :(得分:0)

试试这个:

$(document).ready(function() {

    $('rel[close]').click(function() {
        $(this.parent).close();
    });

    $('a[rel=track]').click(function() {
        var track = this.attr('href');
        track = track.replace(/^.*#/, ''); // remove the hash part of tag id
        var data = 'track=' + track + '&user=<? echo $user; ?>';
        $.ajax({
            url: 'purchase.php',
            type: 'GET',
            data: data,
            cache: false,
            success: function (html) {
                $('#purchasePanel').html(html);
                $('#purchasePanel').show();
                }
        });
    });
});