[英]Double false for Inline If-Else with Conditional Operator
I am trying to hide an input field if the td doesn't have a value. 如果td没有值,我试图隐藏输入字段。
I know we can have a condtition like this: 我知道我们可以有这样的条件:
${condition ? true : false}
but how can I have an inline operator like this? 但是我怎么有这样的内联运算符?
${condition ? true : false && false}
Example: 例:
${JsonObject ? JsonObject.Description : " " && $('#textbox').removeattr("style").hide()}
The html is similar to this: html与此类似:
`<td>
${JsonObject ? JsonObject.Description : " " && $('#textbox').removeattr("style").hide()}
</td>
`<div><input id="textbox"></div>
If I'm understanding you right, you want the truthy result to be the description, and the falsy result to be " "
and to have the side-effect of removing the style attribute from #textbox
and hiding it. 如果我理解正确,那么您希望真实的结果作为描述,而虚假的结果作为
" "
并且具有从#textbox
中删除样式属性并将其隐藏的#textbox
。
I strongly recommend not doing that. 我强烈建议您不要这样做。 Side-effects like that are extremely hard to debug and maintain.
这样的副作用很难调试和维护。
But if you really want to, make the side-effect first: 但是,如果您确实要这样做,请先进行副作用:
`<td>
${JsonObject ? JsonObject.Description : $('#textbox').removeattr("style").hide() && " "}
</td>
`<div><input id="textbox"></div>
That works because hide
returns the jQuery object, which is truthy, so the (object) && " "
expression results in " "
. 之所以可行,是因为
hide
返回的是jQuery对象,这是事实,因此(object) && " "
表达式的结果为" "
。 Or as James Long points out, you could use the comma operator instead, and then the return value of hide
wouldn't matter: ${JsonObject ? JsonObject.Description : ($('#textbox').removeattr("style").hide(), " ")}
或者,正如James Long指出的那样,您可以改用逗号运算符,然后
hide
的返回值将无关紧要: ${JsonObject ? JsonObject.Description : ($('#textbox').removeattr("style").hide(), " ")}
${JsonObject ? JsonObject.Description : ($('#textbox').removeattr("style").hide(), " ")}
(you don't actually need those ()
, but it's already confusing enough, and it's even more confusing without them, so...). ${JsonObject ? JsonObject.Description : ($('#textbox').removeattr("style").hide(), " ")}
(您实际上并不需要那些()
,但是它已经很混乱了,甚至更加混乱了没有他们,所以...)。
But again, I wouldn't. 但是,我不会。 Instead, I'd just do:
相反,我只是这样做:
`<td>
${JsonObject ? JsonObject.Description : " "}
</td>
`<div><input id="textbox"></div>
...and then just before or just after this template literal, have ...然后在此模板文字之前或之后
if (!JsonObject) {
$('#textbox').removeattr("style").hide();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.