gpt4 book ai didi

javascript - 输入表单在reactjs中发送空白值

转载 作者:行者123 更新时间:2023-12-01 04:01:49 25 4
gpt4 key购买 nike

我正在尝试使用react来console.log输入值。下面是我写的代码

import React from 'react';
import ReactDOM from 'react-dom';

class App extends React.Component{
constructor() {
super();
this.processHand = this.processHand.bind(this);

}

processHand(e){
e.preventDefault();
const handMoneyReceived = this.handMoney.value;
console.log(handMoneyReceived);

}

render(){
return(
<div>
<form onSubmit = {this.processHand}>
<input type="text"/>
<input type="submit" ref= {ref => this.handMoney = ref}/>
</form>
</div>
)
}
}

ReactDOM.render(<App />, document.getElementById('container'));

console.log(handMoneyReceived) 正在注销空白值,而不是在表单上输入的值。

最佳答案

因为您在错误的字段上使用了 ref,请将其与文本字段一起使用,尝试以下操作:

<input type="text" ref= {ref => this.handMoney = ref}/>

检查工作fiddle:https://jsfiddle.net/mayankshukla5031/k1efLh8e/

关于javascript - 输入表单在reactjs中发送空白值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42141735/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com