Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在append()中使用三元表达式_Javascript_Jquery_Lambda - Fatal编程技术网

Javascript 在append()中使用三元表达式

Javascript 在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=''>

我试图在jQuery
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返回一个数字。。我最好先用数学。。多谢各位