javascript - thinkphp里模版文件js无法使用if condition的问题
PHPz
PHPz 2017-04-11 12:45:08
[JavaScript讨论组]

项目中有个需求是判断商品是否为VIP专享,是的话就带VIP图标,在模版直接读取数据使用if condition是可以实现的,但是在用js实现“点击加载更多”时,在js代码块里无法实现if condition判断,该怎么办?

初始加载代码:

            
            

{$vo.goods_title}

{$vo.goods_title2}

起拍价:¥{$vo.start_price}

倒计时

然后通过ajax实现点击加载更多,代码如下:

                var nStart = 5;
                $('#more').click(function() {
                    var _this = $(this);
                    if (nStart >= {$total}) {
                        _this.text('更多商品正在上架中...');
                        return false;
                    }else{
                        $.post("{:U('articleAjax')}", {start: nStart},function(res) {
                            $.each(res['result'],function(i, item) {
                                _this.before('

'+item.goods_title+'

'+item.goods_title2+'

起拍价:¥'+item.start_price+'

倒计时

'); }); }); nStart += 5; } });

问题就出在js中这个位置,无法实现判断,换了好几种写法都不行,怎么办?

PHPz
PHPz

学习是最好的投资!

全部回复(1)
PHPz

js的ajax。你获取到的数据会在res里面。然后你遍历res['result']。你的判断肯定是js的判断方式啊。三目运算符会吧。?:

就是下面这个样子
_this.before('<section>'+(item.is_vip == 1?'<img src="/Application/Home/View/default/img/vip.png" class="vipPic">':'')+'<a href="__APP__/Home/Goods/goods/id/'+item.gid+'"><p class="totleInfoPic"><img src="/Application/Home/View/default/'+item.goods_thumb+'"></p><p class="totleInfo"><h2>'+item.goods_title+'</h2><p class="fundinginfo"><span>'+item.goods_title2+'</span><b></b></p></p><p class="q_actionBid">起拍价:¥'+item.start_price+'</p><p class="q_actionstatu">倒计时 <span class="counttime" endTime="'+item.start_time+'"></span></p></a></section>');
                        
                        
                        

有多个值需要判断的情况下

var str = "";
switch(item.is_vip){
    case 1:str = '<img src="/Application/Home/View/default/img/vip.png" class="vipPic">';break;
    case 2:str = "12";break;
    case 3:str = "123456789";break;
}
_this.before('<section>'+(str)+'<a href="__APP__/Home/Goods/goods/id/'+item.gid+'"><p class="totleInfoPic"><img src="/Application/Home/View/default/'+item.goods_thumb+'"></p><p class="totleInfo"><h2>'+item.goods_title+'</h2><p class="fundinginfo"><span>'+item.goods_title2+'</span><b></b></p></p><p class="q_actionBid">起拍价:¥'+item.start_price+'</p><p class="q_actionstatu">倒计时 <span class="counttime" endTime="'+item.start_time+'"></span></p></a></section>');                           
                        

有多个值需要判断的情况下,改成function

function is_vip_show(_value){
    switch(_value){
        case 1:return '<img src="/Application/Home/View/default/img/vip.png" class="vipPic">';
        case 2:return = "12";
        case 3:return = "123456789";
    }
    return "";
}
_this.before('<section>'+is_vip_show(item.is_vip)+'<a href="__APP__/Home/Goods/goods/id/'+item.gid+'"><p class="totleInfoPic"><img src="/Application/Home/View/default/'+item.goods_thumb+'"></p><p class="totleInfo"><h2>'+item.goods_title+'</h2><p class="fundinginfo"><span>'+item.goods_title2+'</span><b></b></p></p><p class="q_actionBid">起拍价:¥'+item.start_price+'</p><p class="q_actionstatu">倒计时 <span class="counttime" endTime="'+item.start_time+'"></span></p></a></section>');
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号