我有以下表格:
<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
如何获得用户的输入?
答案 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)
}