简体   繁体   English

表单复选框和单选按钮如何与React.js一起使用?

[英]How does form checkboxes and radio buttons work with React.js?

I'm trying to create a form in one of my React components. 我正在尝试在我的一个React组件中创建一个表单。 Input and Textarea works fine, but I can't even get the checkbox or radio button to show up. 输入和Textarea工作正常,但我什至无法显示复选框或单选按钮。 Neither render, but the labels for them do. 都不渲染,但是它们的标签都可以。 I followed the tutorial on reactjs.org , but with no luck. 我遵循了reactjs.org上的教程,但是没有运气。

Am I missing something? 我想念什么吗?

Here's what I have: 这是我所拥有的:

export default class Example extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
             isChecked: true
        };

        this.handleCheckboxChange = this.handleCheckboxChange.bind(this);
    }

    handleCheckboxChange(event) {
        const target = event.target;
        const value = target.type === 'checkbox' ? target.checked : target.value;
        const name = target.name;

        this.setState({
            [name]: value
        });
    }

    render() {
        return (
            <div>
                <form>
                    <label>
                        <input name="isChecked" 
                               type="checkbox" 
                               checked={this.state.isChecked} 
                               onChange={this.handleCheckboxChange}/>
                    ONE</label>
                    <label>
                        <input type="checkbox" 
                               name="isChecked" 
                               checked={this.state.isChecked} 
                               onChange={this.handleCheckboxChange} />
                    TWO</label>
                </form>
            </div>
        );
    }
}

(Apologies, as I do not have enough reputation to post a comment) (抱歉,我没有足够的声誉来发表评论)

Your question is not clear, but I assume the issue you are facing is that you component is not rendering on screen. 您的问题尚不清楚,但我认为您面临的问题是您的组件未在屏幕上呈现。

There seems to be no problem with your code and the check boxes are showing up correctly. 您的代码似乎没有问题,并且复选框正确显示。 Check out a JSFiddle I tried out. 查看我尝试过的JSFiddle。

HTML HTML

<script src="https://reactjs.org/js/jsfiddle-integration.js"></script>
<div id="container">
    <!-- This element's contents will be replaced with your component. -->
</div>

Javascript 1.7 Javascript 1.7

class Example extends React.Component {
constructor(props) {
    super(props);
    this.state = {
         isChecked: true
    };

    this.handleCheckboxChange = this.handleCheckboxChange.bind(this);
}

handleCheckboxChange(event) {
    const target = event.target;
    const value = target.type === 'checkbox' ? target.checked : target.value;
    const name = target.name;

    this.setState({
        [name]: value
    });
}

render() {
    return (
        <div>
            <form>
                <label>
                    <input name="isChecked" 
                           type="checkbox" 
                           checked={this.state.isChecked} 
                           onChange={this.handleCheckboxChange}/>
                ONE</label>
                <label>
                    <input type="checkbox" 
                           name="isChecked" 
                           checked={this.state.isChecked} 
                           onChange={this.handleCheckboxChange} />
                TWO</label>
            </form>
        </div>
    );
}
}



  class HelloWidget extends React.Component{
    constructor(props) {
      super(props);
    }
    render() {
        return <div className="widget">
             <Example/>
           </div>;
    }
  }

  React.render(<HelloWidget />, document.getElementById('container'));

JSFiddle link to check the output JSFiddle链接检查输出

http://jsfiddle.net/sam_fisher_nexus/jwm6k66c/3982/ http://jsfiddle.net/sam_fisher_nexus/jwm6k66c/3982/

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

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