【发布时间】:2016-10-04 09:54:13
【问题描述】:
我是 reactjs 新手,正在尝试打印输入字段的更新值。我首先尝试的是:
var App = React.createClass({
render() {
return <div>
<h1>Hello, {this.props.name}</h1>
<input type="text" onKeyUp={this.handleChange} />
<p>{this.handleChange}</p>
</div>;
},
handleChange: function(event) {
return event.target.value;
}
});
App = React.createFactory(App);
React.render(
<App name="World" />,
document.getElementById('mount-point'));
但我不明白为什么它不起作用。比我试过这个:CodePen 也许有人可以帮助我立即打印<p> 元素中输入字段的值
【问题讨论】:
-
您需要将更改的值存储到您的
handleChange中的状态(例如this.setState({ myInputValue: event.target.value });)中。然后您可以像这样“打印”p中的值:<p>{this.state.myInputValue}</p>。 -
强烈建议您阅读文档和教程。 facebook.github.io/react/docs/thinking-in-react.html 否则你只是在猜测 API。
-
@ctrlplusb 是的,我正在研究这个,但我认为也许有办法在 handleChange 事件中获得价值
标签: javascript reactjs jsx