【问题标题】:React Getting default value from an input type text and setting it as a stateReact 从输入类型文本中获取默认值并将其设置为状态
【发布时间】:2019-07-20 02:06:30
【问题描述】:

我想为用户填写一些输入文本字段,因为它将是配置的默认值。

如何将其转移到 React 中的状态变量?

我有:

<input type="number" style={header} value = "5" onChange={this.handleChange('blah')}></input> <br />

作为输入字段,您可以看到我将值设置为 5。如何将其传输到位于构造函数中的状态变量 'blah':

    constructor(props) {
    super(props);
    this.state = {
        blah: '',
    }

提前谢谢你。

【问题讨论】:

    标签: html css reactjs


    【解决方案1】:

    你可以这样实现:

    state = {
        blah : "hello"
      }
      handleChange = (e) => {
        this.setState({blah:e.target.value})
      }
      render(){
        return (
          <input type="text" 
          value = {this.state.blah} 
          onChange={this.handleChange}></input> 
    
        );
      }
    

    现场示例:https://codesandbox.io/s/hungry-kare-50cbn

    如需更多了解,您可以参考此文档:https://reactjs.org/docs/forms.html

    【讨论】:

      猜你喜欢
      • 2018-01-14
      • 2021-04-07
      • 1970-01-01
      • 2021-02-26
      • 2021-04-13
      • 2020-06-03
      • 1970-01-01
      • 2014-02-16
      • 2022-01-20
      相关资源
      最近更新 更多