Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
272 views
in Technique[技术] by (71.8m points)

thinkphp里模版文件js无法使用if condition的问题

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

初始加载代码:

            <volist name="goods" id="vo">
            <section>
                <if condition='$vo.is_vip eq 1'>
                <img src="/Application/Home/View/default/img/vip.png" class="vipPic">
                </if>
                <a href="__APP__/Home/Goods/goods/id/{$vo.gid}">
                    <div class="totleInfoPic"><img src="/Application/Home/View/default/{$vo.goods_thumb}"></div>
                    <div class="totleInfo">
                        <h2>{$vo.goods_title}</h2>                        
                        <div class="fundinginfo"><span>{$vo.goods_title2}</span><b></b></div>
                    </div>
                    <div class="q_actionBid">起拍价:¥{$vo.start_price}</div>
                    <div class="q_actionstatu">倒计时 <span class="counttime" endTime="{$vo.start_time}"></span></div>
                </a>
            </section>
            </volist>

然后通过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('<section><if condition='item.is_vip eq 1'><img src="/Application/Home/View/default/img/vip.png" class="vipPic"></if><a href="__APP__/Home/Goods/goods/id/'+item.gid+'"><div class="totleInfoPic"><img src="/Application/Home/View/default/'+item.goods_thumb+'"></div><div class="totleInfo"><h2>'+item.goods_title+'</h2><div class="fundinginfo"><span>'+item.goods_title2+'</span><b></b></div></div><div class="q_actionBid">起拍价:¥'+item.start_price+'</div><div class="q_actionstatu">倒计时 <span class="counttime" endTime="'+item.start_time+'"></span></div></a></section>');
                            });
                        });
                        nStart += 5;
                    }
                });

问题就出在js中<if condition='item.is_vip eq 1'>这个位置,无法实现判断,换了好几种写法都不行,怎么办?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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+'"><div class="totleInfoPic"><img src="/Application/Home/View/default/'+item.goods_thumb+'"></div><div class="totleInfo"><h2>'+item.goods_title+'</h2><div class="fundinginfo"><span>'+item.goods_title2+'</span><b></b></div></div><div class="q_actionBid">起拍价:¥'+item.start_price+'</div><div class="q_actionstatu">倒计时 <span class="counttime" endTime="'+item.start_time+'"></span></div></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+'"><div class="totleInfoPic"><img src="/Application/Home/View/default/'+item.goods_thumb+'"></div><div class="totleInfo"><h2>'+item.goods_title+'</h2><div class="fundinginfo"><span>'+item.goods_title2+'</span><b></b></div></div><div class="q_actionBid">起拍价:¥'+item.start_price+'</div><div class="q_actionstatu">倒计时 <span class="counttime" endTime="'+item.start_time+'"></span></div></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+'"><div class="totleInfoPic"><img src="/Application/Home/View/default/'+item.goods_thumb+'"></div><div class="totleInfo"><h2>'+item.goods_title+'</h2><div class="fundinginfo"><span>'+item.goods_title2+'</span><b></b></div></div><div class="q_actionBid">起拍价:¥'+item.start_price+'</div><div class="q_actionstatu">倒计时 <span class="counttime" endTime="'+item.start_time+'"></span></div></a></section>');

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...