React(0.13) 定义一个checked组件

<!DOCTYPE html>
<html>
    <head>
        <title>React JS</title>
        <script src="../build_0.13/react.js"></script>
        <script src="../build_0.13/JSXTransformer.js"></script>
    </head>
    <body>
        <div id="example">
        
        </div>
        
        <script type="text/jsx">
            var Checked = React.createClass({
                    //给他一个初始值
                    getInitialState:function(){
                        return {checked:true};
                    },
                    //有改变的时候
                    handleChange:function(event){
                        this.setState({checked:event.target.checked});
                    },
                    //提交
                    submitHandler:function(event){
                        event.preventDefault();
                        alert(this.state.checked);
                    },
                    //渲染html
                    render:function(){
                        return (
                            <form onSubmit={this.submitHandler} >
                                <input type="checkbox" value="A" checked={this.state.checked} onChange={this.handleChange} />
                                <br/>
                                <button type="submit">speak</button>
                            </form>
                        );
                    }
                });

            //将组件加到对应的元素上
            React.render( <Checked />, document.getElementById('example') );
        </script>
    </body>
</html>

 

你可能感兴趣的:(React(0.13) 定义一个checked组件)