jqueryif/else语句
我试图在jQueryAjax方法中编写一些if/else条件。我不太了解jQuery,所以我可能只是犯了一个愚蠢的小语法错误。但这是:jqueryif/else语句,jquery,ajax,if-statement,Jquery,Ajax,If Statement,我试图在jQueryAjax方法中编写一些if/else条件。我不太了解jQuery,所以我可能只是犯了一个愚蠢的小语法错误。但这是: function swapContent(count,product) { $.ajax( { type: "POST", dataType: 'json', url: "includes/pri
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>
编辑四:通过将phpfetchall
方法更改为fetchall(PDO::FETCH_ASSOC)
下面的固定JSON数据结果,修复了获取两组数据时的JSON数据。但仍然无法在库存表字段中获得正确的结果。
至少有两个问题:
$(“.price data”)之前返回。附加部分
数据
(列表)行事,而不是列表中的任何特定元素 $.each(data, function(i, element)
{
if (element.InStock == "In Stock") {
我添加了一些模拟JSON,第一个中的
Unavailable
拼写错误。这只是AJAX成功函数中正确的部分。你可以看到它正在工作
希望有帮助。
R
JsonData=[{
“库存”:“库存中”,
“商品名称”:“便宜咖啡”,
“页面URL”:”http://www.google.com",
“价格”:“14.99”
}, {
“InStock”:“不可用”,
“商品名称”:“药店”,
“页面URL”:”http://www.google.com",
“价格”:“15.99”
}, {
“InStock”:“否”,
“商品名称”:“药店2”,
“页面URL”:”http://www.google.com",
“价格”:“29.99”
}];
$.each(JsonData,函数(索引,数据项){
股票状态='';
如果(dataItem.InStock==“库存”){
stockStatus=“是”;
}else if(dataItem.InStock==“不可用”){
stockStatus=“否”;
}否则{
stockStatus=“-”;
}
tempRow=document.createElement('tr');
$(tempRow).append(“+dataItem.MerchantName+”)
.append(“$”+数据项.Price+”)
.append(“+stockStatus+”)
.附加(“”)
.附加(“”);
$(“.price data”).append(tempRow);
});
您的意思是如果
而不是第二个如果
?您的错误是什么,没有返回任何内容?我认为在返回语句中不需要括号。首先尝试将这些内容取出。您需要指定要将其回显的位置,例如
将是HTML页面中某个位置的容器,然后您可以这样做,而不是返回(“Yes”)
您可以这样做$('.response')。text('Yes')
No,最好使用它:$(“#idname”).html(“写这个”)代码>这会更像它!因为jQuery有自己的API。通过回显数据,您的意思是写入变量或您得到的响应?在这种情况下,返回不是正确的输出方式。考虑将消息建立在变量上,然后将其附加为行,就像您在IF/EL下面做的那样。
$.each(data, function(i, element)
{
if (element.InStock == "In Stock") {
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);
});