如果/ else Javascript语句的行为不符合预期

时间:2015-09-06 16:51:08

标签: javascript jquery

我使用ajax查询数据库并对结果进行了json_encoded。以下是其中一个项目的示例。比较biduser_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>'
                );
            });

2 个答案:

答案 0 :(得分:0)

作为回应,user_pointsbid都在字符串中,请将其转换为整数并将其传递给条件。

if(parseInt(data[i].user_points) < parseInt(data[i].bid))

答案 1 :(得分:0)

您的JSON将user_pointsbid作为字符串返回,您希望它们作为数字返回。您应该更改PHP以不同方式发送数据。

虽然,如果您无法更改PHP,可以更改您的Javascript以进行转换:

&#13;
&#13;
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;
&#13;
&#13;

简短代码:

Number(data[i].user_points) < Number(data[i].bid)