Javascript 以编程方式更改Redux表单复选框字段值
我有一张登记表。当用户接受服务条款时,他单击选中复选框的按钮。当他单击“拒绝”时,此按钮取消选中复选框 我读书。我可以更改除复选框以外的任何字段 这是我的代码:Javascript 以编程方式更改Redux表单复选框字段值,javascript,reactjs,redux-form,Javascript,Reactjs,Redux Form,我有一张登记表。当用户接受服务条款时,他单击选中复选框的按钮。当他单击“拒绝”时,此按钮取消选中复选框 我读书。我可以更改除复选框以外的任何字段 这是我的代码: class RegisterContainer extends Component { constructor(props) { super(props); this.state = { modalTOS: false }; this.accept = this.accept.bin
class RegisterContainer extends Component {
constructor(props) {
super(props);
this.state = {
modalTOS: false
};
this.accept = this.accept.bind(this);
this.decline = this.decline.bind(this);
this.toggleTOSModal= this.toggleTOSModal.bind(this);
}
accept() {
this.props.actions.change("register", "email", "foo42bar"); //This is a working test
this.props.actions.change("register", "read", true);
this.toggleTOSModal();
}
decline() {
this.props.actions.change("register", "read", false);
this.toggleTOSModal();
}
// ....
我添加这行只是为了检查电子邮件值是否可以更改:
this.props.actions.change("register", "email", "foo42bar");
它起作用了
一步一步地,我尝试了很多选项来检查,但没有人更改复选框:
this.props.actions.change("register", "read", "on");
this.props.actions.change("register", "read", true);
this.props.actions.change("register", "read", "1");
this.props.actions.change("register", "read", 1);
this.props.actions.change("register", "read", "true");
this.props.actions.change("register", "read", "a");
在validator中,我添加了一些控制台。错误。通过手动检查或通过“接受”按钮,值为真。但是不要检查
编辑:我的字段声明:
<Field name="read" component={FormCheckBoxGroup} {...fieldProps} onClick={onClickTos} required />
FormCheckBoxGroup:
<FormGroup check>
<Col sm={{ size: 8, offset: 4 }}>
<Label check className={className}>
<Input {...input} type="checkbox" disabled={disabled} required={required} />
{label}
</Label>
{!required && helpBlock && <HelpBlock>{helpBlock}</HelpBlock>}
{error && <HelpBlockErrors errors={messages} />}
{children}
</Col>
</FormGroup>
{label}
{!必需的&&helpBlock&&{helpBlock}
{错误&&}
{儿童}
有人有想法吗?只有在您告诉redux表单这是一个复选框时,true/false才会起作用。从它的文档:
input.checked:布尔值[可选]
仅当值为布尔值时,值的别名。为方便将整个字段对象分解为表单元素的支柱而提供
确保正确声明了字段
<Field name="accpetTerms" component="input" type="checkbox" />
非常感谢!我编辑了您的答案,因为我在子组件中指定了类型。这还不够,我还必须在字段中指定它。Redux-Form中充满了这样的好东西。在我注意到这一点之前,我无法告诉你我经历了多少头部撞击由于初始化问题,我正在将redux应用程序从rc表单迁移到redux表单包。我很确定Redux表单不会为我节省时间,但我希望应用程序会更稳定。谢谢你的帮助!这很有效。但是在initialValue中发送呢,即
defaultChecked={true}。我原以为这样行得通,但事实并非如此Not@Mbanda您没有意识到的是,“发送初始值”与设置“defaultChecked”是一个非常不同的问题(虽然很糟糕,但这是一个令人悲伤的现实)。前者相当容易,而后者则很棘手,因此更难。这绝对不足以在评论中涵盖,你应该将其作为一个单独的问题发布。