美文网首页
13_组件_收集表单数据

13_组件_收集表单数据

作者: zJ_16 | 来源:发表于2018-10-26 16:44 被阅读0次
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
    <title>Title</title>
    <link rel="stylesheet" href="react/mui.min.css">
    <script src="react/react.js"></script>
    <script src="react/react-dom.js"></script>
    <script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"></script>
</head>
<body>
<div id="app"></div>
<script type="text/babel">
    class LoginForm extends React.Component {
        constructor(props) {
            super(props)
            this.state = {
                name: 'name',
                pwd: 'pwd'
            }
            this.handleSubmit = this.handleSubmit.bind(this)
            this.handChange = this.handChange.bind(this)
        }

        handleSubmit(event) {
            //阻止默认行为
            event.preventDefault()
            //console.log(event.target)
            const name = this.nameInput.value
            const pwd = this.state.pwd
            console.log('输入框的值', name, pwd)
        }

        handChange(event) {
            console.log('handChange', event.target.value)
            //读取值
            const pwd = event.target.value;
            //设置值
            this.setState({pwd})
        }

        render() {
            return (
                <div>
                    <form action="http://www.baidu.com" onSubmit={this.handleSubmit}>
                        <input type="text" ref={input => this.nameInput = input}/>
                        <input type="text" value={this.state.pwd} onChange={this.handChange}/>
                        <input type="submit" value="提交"/>
                    </form>

                </div>
            )
        }
    }

    ReactDOM.render(<LoginForm/>, document.getElementById('app'));
</script>
</body>
</html>

相关文章