Javascript 单击按钮即可清除表单
我正试图用react清除表单onClick。事件会触发,但当我尝试将state设置为“”时,它告诉我无法设置未定义的状态 我尝试了几种方法将状态设置为空Javascript 单击按钮即可清除表单,javascript,reactjs,Javascript,Reactjs,我正试图用react清除表单onClick。事件会触发,但当我尝试将state设置为“”时,它告诉我无法设置未定义的状态 我尝试了几种方法将状态设置为空 onHandleChange(e) { this.setState({ input: e.target.value }); } clearForm(e) { e.preventDefault(); const input = this.state.input; console.log("input", input); this.setStat
onHandleChange(e) {
this.setState({
input: e.target.value
});
}
clearForm(e) {
e.preventDefault();
const input = this.state.input;
console.log("input", input);
this.setState({
input: ""
});
}
我还试着用onClick={(this.form.elements[“text input”].value=”“)}
进行内联操作
<button
className="reset btn btn-danger"
type="reset"
id="resetInput"
value="reset"
onChange={this.onHandleChange}
onClick={this.clearForm}
>
Reset
</button>
重置
我的预期输出是输入的值应该为零。空字符串。然而,这并没有发生。。。我没有重置状态。发生这种情况是因为您没有将其正确绑定到
clearForm
函数
无法设置未定义的状态
。此错误意味着在This.state
中,此未定义
使用箭头
功能正确绑定此
上下文
试试这个代码
clearForm = (e) => {
这是因为您没有将其正确绑定到
clearForm
函数
无法设置未定义的状态
。此错误意味着在This.state
中,此未定义
使用箭头
功能正确绑定此
上下文
试试这个代码
clearForm = (e) => {
试试这个代码
<script>
$(document).ready(function(){
$('#btnClear').click(function(){
if(confirm("Want to clear?")){
/*Clear all input type="text" box*/
$('#form1 input[type="text"]').val('');
}
});
});
</script>
$(文档).ready(函数(){
$('#btnClear')。单击(函数(){
如果(确认(“要清除?”){
/*清除所有输入类型=“文本”框*/
$('#form1 input[type=“text”]).val('';
}
});
});
试试这段代码
<script>
$(document).ready(function(){
$('#btnClear').click(function(){
if(confirm("Want to clear?")){
/*Clear all input type="text" box*/
$('#form1 input[type="text"]').val('');
}
});
});
</script>
$(文档).ready(函数(){
$('#btnClear')。单击(函数(){
如果(确认(“要清除?”){
/*清除所有输入类型=“文本”框*/
$('#form1 input[type=“text”]).val('';
}
});
});
单击清除按钮,可以将表单输入的所有值的状态设置为空字符串 单击清除按钮,可以将表单输入的所有值的状态设置为空字符串 我看不到输入文本框,我所看到的只是您在其上应用了onChange
的按钮,该按钮不应存在。我看不到输入文本框的可能重复,我所看到的只是一个按钮,您在它上面应用了onChange
,而这个按钮不应该存在。在类构造函数中可能复制或绑定这个函数this.clearForm=this.clearForm.bind(this)
或者在类构造函数中绑定这个函数this.clearForm=this.clearForm.bind(this)