一、不可控組件和可控組件 不可控組件:<input type="text" defaultValue="HelloWorld"/>資料寫死。 可控組件:<input type="text" defaultValue={this.state.value}/> 為什麼組件要可控: 符合React的資料流 資料存放區在state中,便於使用 便於對資料進行處理
不可控組件代碼:
<!DOCTYPE html><html lang="zh-cn"><head> <meta charset="UTF-8"> <title>表單詳解</title></head><body><script src="./build/react.js"></script><script src="./build/JSXTransformer.js"></script><script type="text/jsx"> /*var MyForm = React.createClass({ render:function () { return <input type="text" defaultValue="HelloWorld!"/> } });*/ var MyForm = React.createClass({ submitHandler:function (event) { event.preventDefault(); var HelloTo = React.findDOMNode(this.refs.helloTo).value;alert(HelloTo); }, render:function () { return <form onSubmit={this.submitHandler}> <input type="text" ref="helloTo" defaultValue="Hello World"/><br/> <button type="submit">Speak</button> </form> } }); React.render(<MyForm></MyForm>,document.body );</script></body></html>
可控組件代碼:
<!DOCTYPE html><html lang="zh-cn"><head> <meta charset="UTF-8"> <title>表單詳解</title></head><body><script src="./build/react.js"></script><script src="./build/JSXTransformer.js"></script><script type="text/jsx"> var MyForm = React.createClass({ getInitialState:function () { return{ helloTo:"helloworld" }; }, handleChange:function () { this.setState({ helloTo:event.target.value }); }, submitHandler:function (event) { event.preventDefault(); alert(this.state.helloTo); }, render:function () { return <form onSubmit={this.submitHandler}> <input type="text" value={this.state.helloTo} onChange={this.handleChange}/><br/> <button type="submit">Speak</button> </form> } }); React.render(<MyForm></MyForm>,document.body );</script></body></html>
二、不同表單元素的使用 <label htmlFor="name">Name</label> <input type="checkbox" value="A" checked={this.state.checked} onChange={this.handleChange}/> <textarea value={this.state.helloTo} onChange={this.handleChange}/> <select value={this.state.helloTo} onChange={this.handleChange}> <option value="one">1</option></select> 執行個體代碼如下:
<!DOCTYPE html><html lang="zh-cn"><head> <meta charset="UTF-8"> <title>表單詳解</title></head><body><script src="./build/react.js"></script><script src="./build/JSXTransformer.js"></script><script type="text/jsx"> var MyForm = React.createClass({ getInitialState:function () { return{ username:"", gender:"man", checked:true }; }, handleUsernameChange:function () { this.setState({ username:event.target.value }); }, handleGenderChange:function (event) { this.setState({ gender:event.target.value }); }, handleCheckboxChange:function (event) { this.setState({ checked:event.target.checked }); }, submitHandler:function () { event.preventDefault(); console.log(this.state); }, render:function () { return <form onSubmit={this.submitHandler}> <label htmlFor="username">請輸入使用者名稱:</label> <input id="username" type="text" value={this.state.username} onChange={this.handleUsernameChange}/><br/> <select value={this.state.gender} onChange={this.handleGenderChange}><option value="man">男</option> <option value="woman">女</option></select> <br/> <label htmlFor="checkbox">同意使用者協議:</label> <input id="checkbox" type="checkbox" value="agree" checked={this.state.checked} onChange={this.handleCheckboxChange}/> <button typ