Skip to content

Instantly share code, notes, and snippets.

@happypeter
Created July 17, 2017 07:20

Revisions

  1. happypeter created this gist Jul 17, 2017.
    30 changes: 30 additions & 0 deletions hello.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    现在来实现表单提交事件的相应,添加 `handleSubmit` 如下

    ```
    <form onSubmit={this.handleSubmit} className="comment-form">
    <input type="text" className="input" />
    <button type="submit" className="submit-btn" >提交</button>
    </form>
    ```

    名称注释:

    - Submit 提交
    - handle 处理
    - form 表单


    然后定义 handleSubmit

    ```js
    handleSubmit = (e) => {
    e.preventDefault()
    console.log('submitting....')
    }
    ```

    由于使用了 ES6 的胖箭头函数语法,所以使用中,就不需要 bind(this) 了。

    `e.preventDefault()` 的作用是

    >在表单提交的时候,阻止页面跳转