简体   繁体   English

内联If-Else(带条件运算符)的双精度false

[英]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.

相关问题 有条件的运算符可以吗? :”被视为JavaScript中if-else语句的替代方法? - Can conditional operator “ ? : ” be considered as an alternative to if-else statement in JavaScript? 无法在JavaScript中干燥简写if-else(条件三元运算符) - Cannot DRY short hand if-else (conditional ternary Operator) in javascript 如果没有模板字符串且没有条件(三元)运算符,但带有if-else,此React代码的外观如何? - How would this React code look like without a template string and without a conditional (ternary) operator, but with if-else? 内联if-else,内含JSX中的大量代码 - Inline if-else with big chunks of code in JSX JavaScript if-else 条件始终返回 false - JavaScript if-else condition always returns false 如何在条件if-else语句上执行函数? - How to execute a function on conditional if-else statement? 在JADE中完成if-else条件块 - Finish if-else conditional block in JADE 是三元运算符,if-else或逻辑或更快的javascript? - Is ternary operator, if-else or logical OR faster in javascript? 将三元运算符的变量转换为if-else语句 - convert variable of ternary operator to if-else statement 如何将三元运算符更改为 if-else 语句? - How to change a ternary operator into an if-else statement?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM