1
所以我对React还比较陌生。我想从三个表单输入中获取数据(并最终将其发布到数据库)。我一直在使用互联网来帮忙,但我想在这里尝试我的运气。就在这里,我正在尝试注销输入中的用户文本。从表单反应中提取多个输入的数据
import React from 'react';
export default class AddForm extends React.Component {
constructor(props) {
super(props);
}
handlePlace(e) {
e.preventDefault();
const text = this.place.value.trim();
console.log(text);
}
handleDate(e) {
const text = this.date.value
console.log(text);
}
handleNotes(e) {
const text = this.notes.value
console.log(text);
}
render() {
return(
<form className="form">
<h4>Add a Memory</h4><br></br>
<p className="add-info">Keep track of all the fun and amazing places you
have been.</p>
<input type="text" name="place" placeholder="place" ref={input =>
this.place = input}></input><br></br>
<input placeholder="time" ref={input => this.date = input}></input><br>
</br>
<input className="notes" placeholder="notes" ref={input => this.notes =
input}></input><br></br>
<button className="save" type="button" onClick=
{this.handleSave}>Save</button>
</form>
);
}
handleSave() {
console.log(this.handlePlace)
console.log(this.handleDate)
console.log(this.handleNotes)
}
}
嘿谢谢!我试图运行它注销新的输入,我得到了这个:无法读取属性'handlePlace'null。 – JontheNerd
你是否将它绑定到构造函数中的handleplace? –
我做到了。它的设置就像你提供的一样。 – JontheNerd