• react 表单


    受控组件:

    import React from 'react'; class CommentBox extends React.Component{ constructor(props){ super(props) this.state = { value: '' } this.handleChange = this.handleChange.bind(this) this.handleSubmit = this.handleSubmit.bind(this) } handleChange(event){ console.log(event.target.value) this.setState({ value: event.target.value }) } handleSubmit(event){ console.log(this.state.value) event.preventDefault(); //阻止默认事件 } render() { return( <form className="p-5" onSubmit={this.handleSubmit}> <div className="form-group"> <label>留言:</label> <input type="text" className="form-control" value={this.state.value} onChange={this.handleChange}> </input> </div> <button type="submit" className="btn btn-primary">提交</button> </form> ) } } export default CommentBox
    非受控组件:

    import React from 'react'; class CommentBox extends React.Component{ constructor(props){ super(props) this.submitHandle = this.submitHandle.bind(this) } submitHandle(event){ console.log(this.textInput.value); event.preventDefault(); } render() { return( <form className="p-5" onSubmit={this.submitHandle}> <div className="form-group"> <label>留言:</label> <input ref={ (textInput) => {this.textInput = textInput}} type="text" className="form-control" > </input> </div> <button type="submit" className="btn btn-primary">提交</button> </form> ) } } export default CommentBox
  • 相关阅读:
    移植spdylay到libcurl
    用到的C++标准库
    libcurl底层调用逻辑
    socket编程
    linux的一些机制Signal, Fork,
    openssl 编程
    对称加密,非对称加密
    ajax提交整个form表单
    一道基础的for语句js编译过程
    怎样将浏览器一句话变为文本编辑器
  • 原文地址:https://www.cnblogs.com/150536FBB/p/13905374.html
Copyright © 2020-2023  润新知