发生了一些奇怪的事情。我使用foundation switch作为我的复选框。无法获取正确的复选框状态
当我在反应浏览器工具上查看我的状态时,我的检查状态为true。当我在控制台中记录状态时,它是错误的。错误在哪里?
ES6:
constructor(props){
super(props);
this.state = {
checked: false
},
this._handleChange = this._handleChange.bind(this)
}
_handleChange(){
this.setState({ checked: !this.state.checked });
console.log(this.state.checked); // becomes the opposite the state!
}
渲染:
<div className="switch">
<input className="switch-input" id="exampleSwitch" type="checkbox" onChange={this._handleChange} checked={this.state.checked} name="exampleSwitch">
<label className="switch-paddle" htmlFor="exampleSwitch">
<span className="show-for-sr">Download Kittens</span>
</label>
</div>
点击时,我的反应控制台显示它作为true
但在控制台,它的false
。 console.log()
显示与该州相反。如果状态为false
,则日志显示true
。任何原因?
编辑为另一个方法:
_onSubmit(){
let checked = this.state.checked;
$.ajax({
...
data: {"checked": checked },
...
});
}
啊。好。但我需要知道,如果状态是错误的,我可以用ajax发送。 – Sylar
谢谢。在使用ajax调用的另一种方法中,我可以安全地使用'this.state.checked'? – Sylar
@Sylar请更新你的问题,我不知道你在说什么'另一种方法,它看起来如何等等。谢谢! –