2013-08-19 251 views
2

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

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; 
} 
?> 

EDIT四:从通过改变PHP下面固定JSON数据的fetchall方法fetchall(PDO::FETCH_ASSOC)结果得到两套数据的固定JSON数据。尽管如此,仍然没有在库存表字段中获得正确的结果。 Fixed JSON Data

+1

你是不是指'else if'代替你的第二个'if'? – brbcoding

+0

你的错误是什么,没有返回?我不认为你在回报声明中需要括号。尝试先取出这些。 – user1477388

+1

你会想要指定你想要回显的地方,例如 '

'将会是HTML页面的某个地方的容器,然后你可以这样做而不是'return(“是”);'你会这样做' $('。response')。text('Yes');' –

回答

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); 
}); 
0

至少存在两个问题:

  1. 你是你之前返回做$(".price-data").append部分
  2. 你是作用于data(列表),而不是列表的任何特定元素。

应该是这样的:

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