Cek*_*eek 11 javascript reactjs react-jsx
我目前有这个简单的反应应用程序,我不能让这些onchange事件为我的生命开火.
var BlogForm = React.createClass({
getInitialState: function() {
return {
title: '',
content: ''
};
},
changeTitle: function(event) {
var text = event.target.value;
console.log(text);
this.setState({
title: event.target.value
});
},
changeContent: function(event) {
this.setState({
content: event.target.value
});
},
addBlog: function(ev) {
console.log("hit hit");
},
render: function() {
return (
<form onSubmit={this.addBlog(this)}>
<div>
<label htmlFor='picure'>Picture</label>
<div><input type='file' id='picture' value={this.state.picture} /></div>
</div>
<div>
<input className="form-control" type='text' id='content' value={this.state.title} onChange={this.changeTitle} placeholder='Title' />
</div>
<div>
<input className="form-control" type='text' id='content' value={this.state.content} onChange={this.changeContent} placeholder='Content' />
</div>
<div>
<button className="btn btn-default">Add Blog</button>
</div>
</form>
);
}
});
Run Code Online (Sandbox Code Playgroud)
有趣的是,当我使用时onChange={this.changeTitle (this)},事件触发但changeTitle函数中的ev变量不正确.
zvo*_*ona 12
尝试:
onChange={(evt) => this.changeTitle(evt)}
Run Code Online (Sandbox Code Playgroud)
要么:
onChange={this.changeTitle.bind(this)}
Run Code Online (Sandbox Code Playgroud)
代替:
onChange={this.changeTitle}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20374 次 |
| 最近记录: |