繁体   English   中英

以编程方式更改Redux-Form复选框字段的值

[英]Programatically change Redux-Form Checkbox Field value

我有一个注册表。 当用户接受服务条款时,他单击一个按钮,选中该复选框。 当他单击拒绝时,此按钮取消选中该复选框。

复选框 接受按钮

我阅读了此答案,以编程方式更改redux-form值 我可以更改除复选框以外的任何字段。

这是我的代码:

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");

在验证器中,我添加了一些console.error 通过手动检查或通过接受按钮,值是true。 但是Checkbox不检查

编辑:我的字段声明:

<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>

有想法吗?

仅当您告诉redux-form这是一个复选框时,true / false才起作用。 从它的文档:

input.checked:boolean [可选]仅当value为布尔值时,才是value的别名。 提供了将整个字段对象分解为表单元素的便利。

确保正确声明您的Field

<Field name="accpetTerms" component="input" type="checkbox" />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM