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

时间:2017-02-09 16:19:18

标签: javascript reactjs react-jsx

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

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)正在注销空白值而不是在表单上输入的值。

1 个答案:

答案 0 :(得分:2)

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

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

检查工作fiddlehttps://jsfiddle.net/mayankshukla5031/k1efLh8e/

相关问题