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