我有一个获取 Prop 的组件 A:[] 乙:[] 值可能是 1、2、3、4
我有 8 个复选框,name="a"和 "b"的值分别为 value="1"、"2"、"3"和 "4" 如果是 onClick 或 onChange,我可以通过 event.target.value 和 event.target.name 获取它们
是否可以设置 defaultChecked with 函数? 像
<input name="a" type="checkbox" value="1" defaultChecked={this.checkIt.bind(this,name,value)} />
checkIt(this,name.value) {
if (this.props.name.indexOf(value)>=0)
{
return true;
}
return false;
}
最佳答案
Is it possible to set the defaultChecked with function?
是的,这是可能的,但您不需要绑定(bind)任何东西,只需调用一个函数并根据条件返回 true 或 false。不需要绑定(bind)函数,因为您要调用该函数并将返回的值分配给 defaultChecked
。
像这样:
defaultChecked={this.checkIt(name,value)}
checkIt(name, value) {
if (this.props.name.indexOf(value) >= 0)
return true;
return false;
}
有关详细信息,请查看此答案:Why is JavaScript bind() necessary?
关于javascript - 是否可以使用函数设置 DefaultCheck? react ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48065949/