Textarea值和文本属性在react中没有定义

textarea value and text attributes goes on undefined in react

本文关键字:定义 react 文本 属性 Textarea      更新时间:2023-09-26

我有一个textarea

<textarea ref="newText" defaultValue={this.props.children}></textarea>

我需要获取它的值,我试过了。

this.props.update(this.refs.newText.value,this.props.index);

我也用text代替value,但它仍然返回undefined

this.refs.newText.value

尝试更新你的react-dom库。Ref帮助您在不需要使用props的情况下强制修改子对象。从react docs获取最新版本

src="https://unpkg.com/react@15/dist/react.js"> src="https://unpkg.com/react-dom@15/dist/react-dom.js">

查看Refs &DOM

如果没有组件的完整源代码,很难进行调试。我准备了一个代码依赖,它显示了访问<textarea/>元素中输入的值的两种方法:http://codepen.io/PiotrBerebecki/pen/amJAqb

也可以看看React Forms文档:https://facebook.github.io/react/docs/forms.html#controlled-components

const TextArea = React.createClass ({ 
  getInitialState: function() {
    return {
      userInputValue: '',
      userInputRefs: ''
    };
  },
  handleChange: function(event) {
    this.setState({
      userInputValue: event.target.value
    });
    this.setState({
      userInputRefs: this.refs.userData.value
    });   
  },
  render: function() {
    return (
      <div>
        <textarea ref="userData"
                  type="text"
                  onChange={this.handleChange}
                  value={this.state.userInputValue} />
        <h3>User input value:</h3>
        <p>{this.state.userInputValue}</p>
        <h3>User input refs:</h3>
        <p>{this.state.userInputRefs}</p>
      </div>
    );
  }
})

ReactDOM.render(
  <TextArea />,
  document.getElementById('app')
)