显然长度很重要-javascript长度问题

显然长度很重要-javascript长度问题,javascript,jquery,Javascript,Jquery,如果db调用中的字符串长度不是0,我将尝试显示div 所以,为了测试,我尝试: html += '<div class="item_image"> price '+image.item_price; if(image.item_price.length>0){ // price html += '<div class="price_holder">'+image.item_price+'</div>'; } // pri

如果db调用中的字符串长度不是0,我将尝试显示div

所以,为了测试,我尝试:

html += '<div class="item_image"> price '+image.item_price;
if(image.item_price.length>0){ // price
        html += '<div class="price_holder">'+image.item_price+'</div>';
        } // price
因此,为了显示我想要的div,因为price字段不是空的,我正在尝试:

html += '<div class="item_image"> price '+image.item_price;
if(image.item_price.length>0){ // price
        html += '<div class="price_holder">'+image.item_price+'</div>';
        } // price
if(image.item\u price.length>0){//price
html+=''+图像。项目价格+'';
}//价格
但是它没有显示div,就好像它没有正确读取长度一样,有没有更好的方法来执行此操作?

您可以按如下所示进行检查

if(image.item_price){ // price
    html += '<div class="price_holder">'+image.item_price+'</div>';
}
if(image.item_price){//price
html+=''+图像。项目价格+'';
}

它将检查
未定义
空字符串
零值
条件

你检查过css了吗?你可以把它放在某个地方。价格持有者{display:none}

html = '';
if (image.item_price.length > 0) {
    html += '<div class="price_holder">' + image.item_price + '</div>';
}
$('body').append(html); // don't forget to append html to where you want it displayed
html=”;
如果(image.item_price.length>0){
html+=''+image.item_price+'';
}
$('body')。附加(html);//不要忘记将html附加到您想要显示的位置

您可以使用
.length
获取字符串的长度。您的设置可能是错误的。你能告诉我们image.item\u price是在哪里实例化的吗?当你控制台.log(image.item\u price.length)时,你看到了什么?你是否尝试使用“undefined”而不是“length”来验证属性是否有值?长度取决于您如何实例化变量。
image.item\u price
可能是一个数字,它没有
Length
属性。@DarrenSweeney,现在我再看一遍,我可以看到您已经附上了货币。但是示例中奖品前面的美元是多少?@ArunPJohny谢谢,正是这样。我确实尝试了if(image.item_price!=''),它工作正常,但您的代码较少。再次感谢。