【问题标题】:ReactJs Checkbox doesn't change value after first useReactJs Checkbox 在第一次使用后不会改变值
【发布时间】:2017-05-09 08:15:45
【问题描述】:

我有一个带有各种表单元素的大型表单,这些表单元素是从 get 请求中动态呈现的。所有其他类型的表单(例如文本和选择)都可以正常工作,但复选框却不行。

在我检查一次后,它只会保持打开状态(即使我取消选中它),我是否在这里遗漏了什么或做错了什么?

这是我目前的相关代码:

class Input extends Component{
    render(){
        var form;
        if (this.props.componentClass=="choice") {
            // select form
        }

        else if (this.props.componentClass=="bool")
            form =(<Checkbox id={this.props.controlId} onChange={this.props.onChange}
                               defaultChecked={this.props.placeholder} >
                        </Checkbox>);
        else
            // text form
        return (
            <div>
                <Form inline onSubmit={this.handleSubmit}>
                    <FormGroup controlId={this.props.controlId}>
                        <ControlLabel>{this.props.name}</ControlLabel>
                        {form}
                        <Panel>
                        {this.props.description}
                        </Panel>
                        <FormControl.Feedback />
                    </FormGroup>
                </Form>
                <br/>
            </div>
        );
    }
}

// onChange code (comes from a parent component)
    onChange(e){
        const form = Object.assign({}, this.state.form);
        form[e.target.id] = e.target.value;
        this.setState({ form });
        console.log('current state: ', this.state);
    }

【问题讨论】:

    标签: javascript forms reactjs checkbox react-bootstrap


    【解决方案1】:

    你必须如前所述绑定onChange函数,但你应该使用“checked”而不是“value”。

    你的例子是这样修改的:

    https://jsfiddle.net/8d3of0e7/3/

    class Input extends React.Component{
    
        constructor(props){
        super(props)
        this.state = {form:{}}
      }
    
        render(){
        var form;
        if (this.props.componentClass=="choice") {
          // select form
        }else if (this.props.componentClass=="bool"){
          form = (
            <ReactBootstrap.Checkbox 
              id={this.props.controlId} 
              onChange={this.props.onChange.bind(this)}
              checked={this.state.form[this.props.controlId]}
              defaultChecked={this.props.placeholder} >
            </ReactBootstrap.Checkbox>);
        }else{
          // text form
        }
        return (
          <div>
            <ReactBootstrap.Form inline onSubmit={this.handleSubmit}>
              <ReactBootstrap.FormGroup controlId={this.props.controlId}>
                <ReactBootstrap.ControlLabel>
                  {this.props.name}
                </ReactBootstrap.ControlLabel>
                {form}
                <ReactBootstrap.Panel>
                  {this.props.description}
                </ReactBootstrap.Panel>
                <ReactBootstrap.FormControl.Feedback />
              </ReactBootstrap.FormGroup>
            </ReactBootstrap.Form>
            <br/>
          </div>
        );
      }
    
      componentDidUpdate(){
        console.log('current state: ', this.state);
      }
    
    }
    
    function onChange(e) {
      const form = Object.assign({}, this.state.form);
      form[e.target.id] = e.target.checked;
      this.setState({ form });
    }
    
    ReactDOM.render(
        <Input componentClass='bool' controlId='retired' 
        name='Is retired?' onChange={onChange}/>,
        document.getElementById('root')
    )
    

    在这个例子中,我们的状态是:state:{form:{retired:true}}

    【讨论】:

    • 完美运行。谢谢!
    【解决方案2】:

    问题是您给复选框一个 onChange 而不将它绑定到一个值。因此,初始检查正在工作,因为这是 defaultChecked 为您所做的,但是一旦您实际与它交互,您就没有绑定到它的状态,导致反应不会在选中或未选中位置重新呈现它。

    【讨论】:

      猜你喜欢
      • 2021-05-31
      • 1970-01-01
      • 1970-01-01
      • 2013-11-14
      • 2014-01-05
      • 1970-01-01
      • 2018-12-17
      • 1970-01-01
      • 2021-10-30
      相关资源
      最近更新 更多