$ .each,jQuery中的if语句 [英] if statement inside $.each, Jquery

查看:189
本文介绍了$ .each,jQuery中的if语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在基于从WebMethod返回的JSON数据构建一些html.我需要根据是否存在partualualr值,知道如何省略某些

元素.到目前为止,我已经知道了,但是,我在VS中出现语法错误

I'm building some html based on JSON data returned from a WebMethod. I need to know how to leave out certain

elements, based on if a particualr value is present or not. I've got this, so far but, i get syntax error within VS

$.each(amlData, function (index, item) {
    $('<div class=message><pre><dl><dt>Matched On:</dt><dd>' + item.ItemType +
    '</dd><dt>Date Listed:</dt><dd>' + item.DateListed +
    '</dd><dt>ID:<dt><dd>' + item.ID +
    '</dd><dt>Percentage:</dt><dd>' + item.PercentageMatch +
    '</dd><dt>List:</dt><dd>' + item.List +
    '</dd>' if(item.Gender != null) { + '<dt>Gender:</dt><dd>' + item.Gender   } +  '</dl></pre></div>').appendTo('div#results');                                  
});

推荐答案

您不能使用这种方法.但您可以这样做:

you cant use if like that. but you can do this:

'</dd>' + (item.Gender != null ? '<dt>Gender:</dt><dd>'+item.Gender : '')+  '</dl>...

这篇关于$ .each,jQuery中的if语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆