简体   繁体   English

react js中的复选框不会更改

[英]Checkbox in react js doesn't change

I have main component as follows : 我的主要组成部分如下:

export default class RegistrationFormStepFour extends React.Component{
constructor(props){
    super(props);
    this.state = {
        terms: false,
        emailNotifications: false,
        smsNotifications: false,
        errors: {}
    }
}


handleTerms(event){
    event.preventDefault();
    this.setState({terms: !this.state.terms});
}

render(){
    const language = this.props.currentLanguage;

    return (
        <div>
            <Terms
                nameYes="chcTerms"
                text={language.termsText}
                state={this.state.terms}
                onChange={this.handleTerms.bind(this)}
                currentLanguage={language}
                error={this.state.errors.terms}/>

        </div>
    );
}
}

And component term is as follows : 组成项如下:

import React from 'react';

const Terms = ({nameYes, text, state, onChange, error}) => {

let hasError = error ? "hasError" : "";

return (
    <div className="col-lg-12 text-center" style={{marginBottom: 30}}>
        <form>
            <label className="radio-inline">
                <input
                    type="checkbox"
                    name={nameYes}
                    checked={state}
                    onChange={onChange}
                    value=""/>
            </label>
        </form>
        <p className={`questionsText ${hasError}`} style={{marginTop: 10}}>{text}</p>
    </div>
);
};

export default Terms;

But when I click on the checkbox, nothing happens. 但是,当我单击复选框时,什么也没有发生。 If I console log the state in the terms component it show right value. 如果我在条件组件中登录控制台state ,则显示正确的值。 First time is false , when I click on the checkbox than is true, but the checkbox isn't checked. 第一次是false ,当我单击复选框时,设置为true,但是未选中该复选框。

Any advice? 有什么建议吗?

The event.preventDefault() is causing your problems in the controlled component checkbox. event.preventDefault()在受控组件复选框中引起您的问题。

http://www.matthiaslienau.de/blog/tags/checkbox http://www.matthiaslienau.de/blog/tags/checkbox

Checkboxes (and Radio Buttons): Manually updating state fails for checkboxes (and without having tested it: I think for radio controls as well). 复选框(和单选按钮):复选框的手动更新状态失败(并且未经测试:我也认为单选按钮也是如此)。 Why? 为什么? The problem one will face is that the checkbox onChange event behaves in a special way since the era of HTML (how could I forget!): You may not toggle the state of a checkbox manually via the .checked property. 一个人将要面对的问题是,自HTML时代以来,复选框onChange事件的行为就具有特殊性(我怎么会忘记!):您可能无法通过.checked属性手动切换复选框的状态。 Nor does React. React也没有。 The onChange (onClick) event is fired after the element state changed internally. 内部更改元素状态后,将触发onChange(onClick)事件。 This may just be reverted based on the return value of the event handler. 可以仅基于事件处理程序的返回值来还原此值。 See this post for a comprehensive examination of this fact. 有关此事实的全面检查,请参见这篇文章。

 const Terms = ({ nameYes, text, state, onChange, error }) => { let hasError = error ? "hasError" : ""; return ( <div> <form> <label className="radio-inline"> <input type="checkbox" name={nameYes} checked={state} onChange={onChange} /> </label> </form> <p className={`questionsText ${hasError}`}>{text}</p> </div> ); }; class RegistrationFormStepFour extends React.Component { constructor(props) { super(props); this.state = { terms: false, emailNotifications: false, smsNotifications: false, errors: {} } } handleTerms(event) { this.setState({ terms: event.target.checked }); } render() { const language = { termsText: 'Some Language' }; return ( <div> <Terms nameYes="chcTerms" text={language.termsText} state={this.state.terms} onChange={this.handleTerms.bind(this)} currentLanguage={language} error={this.state.errors.terms}/> {JSON.stringify(this.state)} </div> ); } } ReactDOM.render(<RegistrationFormStepFour />, document.getElementById('app')) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="app"></div> 

I think you should remove value attr from input tag. 我认为您应该从输入标签中删除值attr。 try this : 尝试这个 :

  <input
                        type="checkbox"
                        name={nameYes}
                        checked={state}
                        onChange={onChange}
                        />

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

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