Javascript 在append()中使用三元表达式
我试图在jQueryJavascript 在append()中使用三元表达式,javascript,jquery,lambda,Javascript,Jquery,Lambda,我试图在jQueryappend()中使用三元表达式。我已经在控制台中进行了检查,没有显示任何错误。问题是tr没有附加到tbody $(".invoice table tbody").append("<tr> \ <td> <span class=''>" + price.toFixed(0) == 0 ? Obs : 1 + "</span></td> \ <td> <span class=''>
append()
中使用三元表达式。我已经在控制台中进行了检查,没有显示任何错误。问题是tr
没有附加到tbody
$(".invoice table tbody").append("<tr> \
<td> <span class=''>" + price.toFixed(0) == 0 ? Obs : 1 + "</span></td> \
<td> <span class=''>" + price.toFixed(2) + "</span> </td> \
<td> <input type='checkbox' name='removeItem' class='removeItemCheckBox'/></td> \
</tr>");
$(“.invoice table tbody”).append(“\
“+price.toFixed(0)==0?Obs:1+”\
“+price.toFixed(2)+”\
\
");
这个?:代码>运算符不是“lambda表达式”;这是条件运算符。问题是它的优先级很低,因此必须将其括起来:
$(".invoice table tbody").append("\
<tr> \
<td> <span class=''>" + (price.toFixed(0) == 0 ? Obs : 1) + "</span></td> \
<td> <span class=''>" + price.toFixed(2) + "</span> </td> \
<td> <input type='checkbox' name='removeItem' class='removeItemCheckBox'/></td> \
</tr> \
");
$(“.invoice table tbody”).append(“\
\
“+(price.toFixed(0)=0?Obs:1)+”\
“+price.toFixed(2)+”\
\
\
");
如果没有括号,两边的+
运算符将优先,整个表达式将不同。“有点不对劲”。什么你必须向我们解释错误的行为来帮助你。代码中到底哪里有“lambda表达式”?@Pointy我认为OP的意思是“三元”,而不是“lambda”。我的意思是这个条件:price.toFixed(0)==0?Obs:1。。。因为我已经设置了这个条件,所以什么都没有发生。你在控制台上看到了什么错误?Obs
是定义变量吗?是的,那不是lambda,是的。我已经测试过了。它是有效的。这些比喻解决了这个问题。。非常感谢。请注意,price.toFixed(0)
是多余的,并且也会导致在比较中进行隐式类型转换。要点:@user2232273请注意,.toFixed(0)
返回的是字符串,而不是数字;您最好使用Math.floor(price)
。谢谢您的建议。我想。toFixed返回一个数字。。我最好先用数学。。多谢各位