我使用ajax查询数据库并对结果进行了json_encoded。以下是其中一个项目的示例。比较bid
和user_points
,我要么显示"没有足够的积分"或"立即购买"按钮。
[
{
"id": "205",
"item_id": "727",
"winner_id": "62",
"bid": "153",
"status": "1",
"date": "2015-09-05 05:53:12",
"exp_date": "2015-09-07 05:53:12",
"user_id": "296",
"item_title": "neptun",
"stime": 84178,
"time_left": "0 d : 23 h : 22 m",
"user_points": "1222",
"user_thumb": "images\/ppic\/15x\/296.jpg?x=timestamp"
}
]
用于追加#pending-wins
内每个元素的Javascript。问题在于data[i].user_points < data[i].bid
。使用上面的例子,它应该显示&#34;立即购买&#34;按钮,而不是&#34;没有足够的点&#34;按钮显示。知道为什么吗?
$.each(data, function(i)
{
$("#pending-wins").append(
'<div class="col-sm-5 col-md-3">\
<div class="thumbnail">\
<a href="item?id='+data[i].item_id+'"><img src="images/355x/'+data[i].item_id+'-1.jpg" alt="" /></a>\
<h3>'+data[i].item_title+'</h3>\
<div class="caption">\
<ul class="list-group">\
<li class="list-group-item">\
<span class="badge">'+data[i].bid+'</span>\
Cost\
</li>\
<li class="list-group-item">\
<span class="badge user-badge"><img src="'+data[i].user_thumb+'" alt="" /></span>\
Seller\
</li>\
<li class="list-group-item">\
<span class="badge">'+data[i].time_left+'</span>\
Time left:\
</li>'
+(data[i].user_points < data[i].bid ?
'<li class="list-group-item">\
<span class="badge background-none"><a href="points"><button>Click here</button></a></badge></span>\
Not enough points for this item.\
</li>'
:
(data[i].stime <= 0 ?
''
:
'<li class="list-group-item center"><button id="claim-'+data[i].item_id+'" class="btn btn-primary btn-sm claim">Claim Win</button></li>'
)
)+
'</ul>\
</div>\
</div>\
</div>'
);
});
答案 0 :(得分:0)
作为回应,user_points
和bid
都在字符串中,请将其转换为整数并将其传递给条件。
if(parseInt(data[i].user_points) < parseInt(data[i].bid))
答案 1 :(得分:0)
您的JSON将user_points
和bid
作为字符串返回,您希望它们作为数字返回。您应该更改PHP以不同方式发送数据。
虽然,如果您无法更改PHP,可以更改您的Javascript以进行转换:
var data =[
{
"id": "205",
"item_id": "727",
"winner_id": "62",
"bid": "153",
"status": "1",
"date": "2015-09-05 05:53:12",
"exp_date": "2015-09-07 05:53:12",
"user_id": "296",
"item_title": "neptun",
"stime": 84178,
"time_left": "0 d : 23 h : 22 m",
"user_points": "1222",
"user_thumb": "images\/ppic\/15x\/296.jpg?x=timestamp"
}
];
$.each(data, function(i)
{
$("#pending-wins").append(
'<div class="col-sm-5 col-md-3">\
<div class="thumbnail">\
<a href="item?id='+data[i].item_id+'"><img src="images/355x/'+data[i].item_id+'-1.jpg" alt="" /></a>\
<h3>'+data[i].item_title+'</h3>\
<div class="caption">\
<ul class="list-group">\
<li class="list-group-item">\
<span class="badge">'+data[i].bid+'</span>\
Cost\
</li>\
<li class="list-group-item">\
<span class="badge user-badge"><img src="'+data[i].user_thumb+'" alt="" /></span>\
Seller\
</li>\
<li class="list-group-item">\
<span class="badge">'+data[i].time_left+'</span>\
Time left:\
</li>'
+(Number(data[i].user_points) < Number(data[i].bid) ?
'<li class="list-group-item">\
<span class="badge background-none"><a href="points"><button>Click here</button></a></badge></span>\
Not enough points for this item.\
</li>'
:
(data[i].stime <= 0 ?
''
:
'<li class="list-group-item center"><button id="claim-'+data[i].item_id+'" class="btn btn-primary btn-sm claim">Claim Win</button></li>'
)
)+
'</ul>\
</div>\
</div>\
</div>'
);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="pending-wins"></div>
&#13;
简短代码:
Number(data[i].user_points) < Number(data[i].bid)