Javascript 将反应文本字段输入值作为参数传递给方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39723391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 22:50:24  来源:igfitidea点击:

Passing react text field input values as parameters to a method

javascriptbuttonreactjsinputtypescript

提问by mayooran

I have the below input fields of which I need to get the entered inputs and pass it to the onClick event of the button shown below.

我有以下输入字段,我需要获取输入的输入并将其传递给如下所示按钮的 onClick 事件。

<input type="text" style={textFieldStyle} name="topicBox" placeholder="Enter topic here..."/>
<input type="text" style = {textFieldStyle} name="payloadBox" placeholder="Enter payload here..."/>
<button value="Send" style={ buttonStyle } onClick={this.publish.bind(this,<value of input field 1>,<value of input field2>)}>Publish</button><span/>

I have a method called publish which takes two string arguments. In place of those strings, I need to pass the values entered in the input fields. How can I achieve this without storing the values in states? I do not want to store the input field values in state variables. Any help would be much appreciated.

我有一个名为 publish 的方法,它接受两个字符串参数。代替这些字符串,我需要传递在输入字段中输入的值。如何在不将值存储在状态中的情况下实现这一目标?我不想将输入字段值存储在状态变量中。任何帮助将非常感激。

回答by Alexander T.

How can I achieve this without storing the values in states?

如何在不将值存储在状态中的情况下实现这一点?

I think in this case better use states

我认为在这种情况下更好地使用状态

class App extends React.Component {
  constructor() {
    super();
    this.state = {
      topicBox: null,
      payloadBox: null
    };
    
    this.publish = this.publish.bind(this);
    this.handleChange = this.handleChange.bind(this);
  }
  
  handleChange({ target }) {
    this.setState({
      [target.name]: target.value
    });
  }

  publish() {
    console.log( this.state.topicBox, this.state.payloadBox );
  }
  
  render() {
    return <div>
      <input 
        type="text" 
        name="topicBox" 
        placeholder="Enter topic here..." 
        value={ this.state.topicBox }
        onChange={ this.handleChange } 
      />
      
      <input 
        type="text" 
        name="payloadBox" 
        placeholder="Enter payload here..."
        value={ this.state.payloadBox } 
        onChange={ this.handleChange } 
      />
      
      <button value="Send" onClick={ this.publish }>Publish</button>
    </div>
  }
}

ReactDOM.render(<App />, document.getElementById('container'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="container"></div>

回答by Muhammad Aref

You can add ref for each text field, and read the value from it like:

您可以为每个文本字段添加 ref,并从中读取值,例如:

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

  publish(topicBox, payloadBox) {
    alert(this.refs.topic.value);
    alert(this.refs.payload.value);
  }

  render() {
    return <div>
      <input 
        ref="topic"
        type="text"
        name="topicBox"
        placeholder="Enter topic here..."/>

      <input 
        ref="payload"
        type="text"
        name="payloadBox"
        placeholder="Enter payload here..."/>

      <button 
        value="Send"
        onClick={this.publish}> 
        Publish
      </button>
    </div>
  }
}

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

Working fiddle https://jsfiddle.net/hjx3ug8a/15/

工作小提琴https://jsfiddle.net/hjx3ug8a/15/

Thanks for Alexander Tfor his addition!

感谢Alexander T的加入!