获取表单提交的结果

时间:2016-05-15 17:21:43

标签: javascript html twitter-bootstrap reactjs

我有以下表格:

<form onSubmit={this.gotEmail}>
  <div className="form-group">
    <FormControl type="text" className="form-control"/>
  </div>
  <Button className="btn btn-primary btn-large centerButton" type="submit">Send</Button>
</form>

提交后,我想在这里写下用户写的字段:

<FormControl type="text" className="form-control"/>

我试着这样做:

gotEmail: function(email) {
    console.log("Email: ", JSON.stringify(email))
}

然而,我收到此错误:

EmailForm.jsx:9 Uncaught TypeError: Converting circular structure to JSON 

当我尝试在控制台中打印email时,我得到了这个:

(program):132 Uncaught illegal access

如何获得用户的输入?

2 个答案:

答案 0 :(得分:0)

在纯JavaScript中,您可能会执行以下操作:

  

HTML:

<form action="getInput()">
<input type="text" id="email" class="form-control/>
</form>
  

JS:

function getInput(){
var email = document.getElementById("email").value;
alert(email);
}

答案 1 :(得分:0)

在React中访问组件的HTML元素的正确方法是:

import { findDOMNode } from 'react-dom'

[...] // all your other script stuff here

render() {
  return (
    <form onSubmit={this.gotEmail}>
      <div className="form-group">
        <FormControl ref="email" type="text" className="form-control"/>
      </div>
      <Button className="btn btn-primary btn-large centerButton" type="submit">Send</Button>
    </form>
  )
}

gotEmail() {
  console.log(findDOMNode(this.refs.email).value)
}
相关问题