jQuery if / else语句

时间:2013-08-19 16:39:45

标签: jquery ajax if-statement

我正在尝试在jQuery ajax方法中编写一些if / else条件。我不太了解jQuery,所以我可能只是犯了一个愚蠢的小语法错误。但这是:

    function swapContent(count,product)
        {
            $.ajax(
            {
                type: "POST",
                dataType: 'json',
                url: "includes/price-update.php",
                data: {countVar: count, ProductID: product},
                success: function(data)
                {
                    console.log(data);
                    $('.cleardata').remove();

                    $.each(data, function ()
                    {

                        $(".price-data").append(
                        $("<tr class='cleardata'/>")

                        if (data.InStock == "In Stock") {
                        $('.in-stock-row').text ('Yes');
                        }
                        else if (data.InStock == "Unavaiable"){
                             $('.in-stock-row').text ('No');
                        }
                        else{
                             $('.in-stock-row').text ('-');
                        }
                        .append("<td class='store-row'><h5 property='seller'>" + this.MerchantName + "</h5></td>")
                        .append("<td class='price-row'><h5 property='price'>$" + this.Price + "</h5></td>")
                        .append("<td class='in-stock-row'><h5>" + this.InStock + "</h5></td>")
                        .append("<td class='merch-row'><a property='url' target='_blank' href='" + this.PageURL + "' class='merch-but'>GET IT</a></td>")
                        );
                    })
                }
            });
        }
    </script>

除了if / else语句之外,一切都很好。我不确定return是否是回显数据的正确方法。我是一个PHP人,所以jQuery仍然是全新的。谢谢你的帮助。

编辑:我刚刚根据一些建议更改了代码,这就是我所拥有的,现在我的表中没有任何内容出现。

第二次编辑:附加JSON数据的图像以进行调试。 JSON Data from ajax post method. Not sure why there is a 3 and InStock with the same properties.

第三次编辑:发布PHP数据

<?php
$countVar = $_POST['countVar'];
$ProductID = $_POST['ProductID'];
$data = PriceCompare($countVar, $ProductID);
echo json_encode($data);

function PriceCompare($countVar, $ProductID){
$DBH = new PDO('mysql:host=localhost;dbname=---','---','---');
$STH = $DBH->query('SELECT MerchantName, Price, PageURL, InStock
                    FROM merchants
                    WHERE ProductID=' . $ProductID .' AND Count=' . $countVar . '
                    ORDER BY Price');

$result = $STH->fetchAll();

return $result;
}
?>

编辑四:通过将php fetchall方法更改为fetchall(PDO::FETCH_ASSOC)以下固定JSON数据的结果,修复了获取两组数据的JSON数据。但是仍然没有在库存表字段中获得正确的结果。 Fixed JSON Data

2 个答案:

答案 0 :(得分:2)

我添加了一些模拟JSON,第一个拼写中Unavailable的拼写错误。这只是AJAX成功函数的each部分是正确的。您可以在http://jsfiddle.net/mhWdP/

上看到这一点

希望有所帮助。 R上。

JsonData = [{
    "InStock": "In Stock",
        "MerchantName": "Coffee for Less",
        "PageURL": "http://www.google.com",
        "Price": "14.99"
}, {
    "InStock": "Unavailable",
        "MerchantName": "Drugstore",
        "PageURL": "http://www.google.com",
        "Price": "15.99"
}, {
    "InStock": "No",
        "MerchantName": "Drugstore2",
        "PageURL": "http://www.google.com",
        "Price": "29.99"
}];


$.each(JsonData, function (index, dataItem) {
    stockStatus = '';
    if (dataItem.InStock == "In Stock") {
        stockStatus = "Yes";
    } else if (dataItem.InStock == "Unavailable") {
        stockStatus = "No";
    } else {
        stockStatus = "-";
    }

    tempRow = document.createElement('tr');

    $(tempRow).append("<td class='store-row'><h5 property='seller'>" + dataItem.MerchantName + "</h5></td>")
        .append("<td class='price-row'><h5 property='price'>$" + dataItem.Price + "</h5></td>")
        .append("<td class='in-stock-row'><h5>" + stockStatus + "</h5></td>")
        .append("<td class='merch-row'><a property='url' target='_blank' href='" + dataItem.PageURL + "' class='merch-but'>GET IT</a></td>")
        .append("</tr>");

    $(".price-data").append(tempRow);
});

答案 1 :(得分:0)

至少有两个问题:

  1. 您在执行$(".price-data").append部分
  2. 之前返回
  3. 您正在data(列表),而不是列表中的任何特定元素。
  4. 应该是这样的:

                    $.each(data, function(i, element)
                    {
                        if (element.InStock == "In Stock") {
    
相关问题