如何编写if语句来确定是否在jsx中显示一个属性 [英] How do I write an if statement to determine whether to display an attribute in jsx
问题描述
这会引发错误。每个选项的意外令牌:
This throws an error. Unexpected token for eachOption:
<input {eachOption===selectedValue?'checked="checked"':''} type="checkbox" name="checkme"/>
推荐答案
替换为 checked =每个选项=== selectedValue}
。
应该这样做。
但是,请确保您还具有附加到复选框的 onChange
处理程序(作为要在React中使用的任何其他输入),否则可能不会像您那样运行
However, make sure you also have the onChange
handler attached to the checkbox (as any other input that you would like to use in React), otherwise it might not behave as you'd expect.
您应该使用JSX设置属性值。
You should set values for attributes using JSX.
通过设置检查
为false,这将意味着该复选框不会被检查。如果条件( eachOption === selectedValue
)成立,那么您将检查该复选框。
By setting the value of checked
to false, it will mean that the checkbox will not be checked. If the condition (eachOption === selectedValue
) holds then the checkbox will be checked for you.
这是一个 JSBin
这篇关于如何编写if语句来确定是否在jsx中显示一个属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!