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

标签 javascript reactjs

我正在尝试使用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/

相关文章:

reactjs - Redux如何处理reducer中的错误

javascript - React如何在iframe中输出名称

javascript - 如何在 socket.io 中锁定全局数组

javascript - 这是 Firebug 中的错误吗?

node.js - 我的 NodeJS 服务器在 6 个 Axios post 请求后崩溃

reactjs - 使用 Jest/Enzyme 延迟后测试 React 组件的意外行为

javascript - Highcharts 条形和线条问题

javascript - 如何在 javaScript 中获取动态 div 宽度

reactjs - 为什么在 React 中返回时需要额外的包装器?

javascript - 在 useEffect() 内部的函数上使用立即返回是否会以任何方式改变行为?