简体   繁体   English

获取表单提交的结果

[英]Get result of form submission

I have the following form: 我有以下表格:

<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>

When submitted, I would like to get the field written by the user here: 提交后,我希望在此处获得用户编写的字段:

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

I tried getting it like this: 我试图这样得到它:

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

However, I get this error: 但是,我收到此错误:

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

When I try to print email in the console I am getting this: 当我尝试在控制台中打印email时,出现以下信息:

(program):132 Uncaught illegal access

How can I get the user's input? 如何获得用户的输入?

In plain JavaScript you would probably do the following : 在普通的JavaScript中,您可能会执行以下操作:

HTML: HTML:

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

JS: JS:

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

The proper way to access an HTML element of your component in React is: 在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)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM