jqueryajax调用在If语句中不起作用

2024-07-03 08:01:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我的Ajax调用在if语句中不起作用有什么原因吗?我可能在这里做了一些根本错误的事情,因为我对JS或Ajax没有太多经验。谢谢

工作函数是:

function filter(varType, varValue) {
    var $products = $('#products');

    $.getJSON($SCRIPT_ROOT + '/filterSize/' + varValue)

    .success(function (data) {
        $products.empty(); // clear html from container
        var elt0 = '<div class="page-content"><h1 id="shoes">Shoes</h1></div>';
        $products.append(elt0);

        if (Object.keys(data.shoes).length === 0) {
            none = '<span class="noresults">No results</span>';
            $products.append(none);
        }

        else {
            var numItems = (Object.keys(data.shoes).length);
            for (index = 0; index < numItems ; ++index) {
                var elt1 = Flask.url_for("static", {"filename": data.shoes[index].img1});
                var elt2 = '<div id="item" class="col-md-4"><div class="products"><a href="shop/item/' + data.shoes[index].id + '"><h5>' + data.shoes[index].name + '</h5><img src="' + elt1 + '" alt="" width="200px" height="125px"></a></div>';
                $products.append(elt2);
            }
        }
    });
}

但如果我这么做,它会突然停止工作:

function filter(varType, varValue) {
    var $products = $('#products');

    var x = 5
    var y = 6

    if (y > x) {
        $.getJSON($SCRIPT_ROOT + '/filterSize/' + varValue)
    }

    .success(function (data) {
        $products.empty(); // clear html from container
        var elt0 = '<div class="page-content"><h1 id="shoes">Shoes</h1></div>';
        $products.append(elt0);

        if (Object.keys(data.shoes).length === 0) {
            none = '<span class="noresults">No results</span>';
            $products.append(none);
        }

        else {
            var numItems = (Object.keys(data.shoes).length);
            for (index = 0; index < numItems ; ++index) {
                var elt1 = Flask.url_for("static", {"filename": data.shoes[index].img1});
                var elt2 = '<div id="item" class="col-md-4"><div class="products"><a href="shop/item/' + data.shoes[index].id + '"><h5>' + data.shoes[index].name + '</h5><img src="' + elt1 + '" alt="" width="200px" height="125px"></a></div>';
                $products.append(elt2);
            }
        }
    });
}

Tags: dividdataindexifobjectvarfunction
2条回答
  if (y > x) {
            $.getJSON($SCRIPT_ROOT + '/filterSize/' + varValue)

        .success(function (data) {
            $products.empty(); // clear html from container
            var elt0 = '<div class="page-content"><h1 id="shoes">Shoes</h1></div>';
            $products.append(elt0);

            if (Object.keys(data.shoes).length === 0) {
                none = '<span class="noresults">No results</span>';
                $products.append(none);
            }

            else {
                var numItems = (Object.keys(data.shoes).length);
                for (index = 0; index < numItems ; ++index) {
                    var elt1 = Flask.url_for("static", {"filename": data.shoes[index].img1});
                    var elt2 = '<div id="item" class="col-md-4"><div class="products"><a href="shop/item/' + data.shoes[index].id + '"><h5>' + data.shoes[index].name + '</h5><img src="' + elt1 + '" alt="" width="200px" height="125px"></a></div>';
                    $products.append(elt2);
                }
            }
        });
}

这是有效的语法。你需要把成功移到括号里。你知道吗

您过早地关闭了if-语句的大括号:

if (y > x) {
    $.getJSON($SCRIPT_ROOT + '/filterSize/' + varValue)
//} <  TOO EARLY
    .success(function (data) {
        ....
    });
} <  SHOULD BE HERE INSTEAD

.success()回调之后结束if语句。你知道吗

相关问题 更多 >