React(0.13) 定義一個checked元件

weixin_34253539發表於2016-03-25
<!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>

 

相關文章