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>

 

posted @ 2016-03-25 15:32  天道酬勤,坚持!  阅读(570)  评论(0编辑  收藏  举报