获取所有值表行输入框

时间:2017-08-30 05:56:50

标签: javascript reactjs

我是新手,我在检索表格中行的输入框值时遇到了一些困难。

在我的渲染方法中,我有一个表格,表格和保存所有按钮,

render() {
    return (
      <div>
      <form onSubmit={this.handleSubmit}>

      { (this.state.inputList.length > 0) ?
        (<div>
          <table id='configtable'>

            <tbody>
              {this.state.inputList}
            </tbody>
          </table>

          <table id="table-footer">
            <tfoot>
            <tr>
              <td />
              <td>
                <input name="fwdAllNumber" type="number" placeholder="Phone Number"  pattern="[0-9]*" inputMode="numeric" onChange={this.handleFwdAllInput}/>
              </td>
              <td>
              <ButtonGroup className="button-group-right">
                <Button className="config-button" type="submit" value="Submit" onClick={this.saveAll}>Save All</Button>
              </ButtonGroup>
              </td>
            </tr>

          </table>
          </div>
        )  : (<div><br/><h4>No Phone Numbers currrently exist. Please click "Add Phone Number" to begin</h4></div>)}
        </form>
        </div>
      )
    }
  })

addRow方法在“添加行”按钮

的按下的渲染方法中添加一行
addRow(event) {
    const inputList = this.state.inputList;
    const index = this.state.index;
    let rows = this.state.rows;

    const row = (
      <tr key={ inputList.length } name={ inputList.length }>
        <td key={index}><input name={'phone_'+index} type="number" placeholder="Foward Phone Number"  pattern="[0-9]*" inputMode="numeric" ref={(input) => this.phone_$index = input} type="text"/> </td>
        <td><input name={'fwd_'+index} type="number" placeholder="Foward Phone Number"  pattern="[0-9]*" inputMode="numeric" ref={(input) => this.fwd_$index = input} /></td>
          <td id="forwarded-indicator">
          <div id="forwarded-indicator-div" />
        </td>
      </tr>
    );
}

当我点击saveAll按钮时,我需要获取所有行中输入框的所有值。

我试过了,

handleSubmit: function(event) {
  event.preventDefault();
  let rows = this.state.rows;
  const tableValues = this.state.inputValueList;

  let configVal = [];
  for(let i = 0; i < rows.length; i++){
    console.log(this.phone_ + i.value);
  }
},

但是我在这里得到了Nan,而且我得到了控制台,

<input type="text" name="phone_0" placeholder="Foward Phone Number" pattern="[0-9]*" inputmode="numeric">
<!-- react-text: 162 -->
<!-- /react-text -->

如果有人能帮助我提交,我将非常感激 谢谢

1 个答案:

答案 0 :(得分:0)

问题在于你在这里为ref分配输入元素:

ref={(input) => this.fwd_$index = input}

使用此:

ref={(input) => this[`fwd_${index}`] = input}

然后使用:

获取值
this[`fwd_${index}`].value

检查工作示例:

&#13;
&#13;
class App extends React.Component{

   submit(e){
      e.preventDefault();
      for(let i=0; i< 4; i++)
        console.log('value', this[`input_${i}`].value);
   }

   render(){
      return(
         <form onSubmit={this.submit.bind(this)}>
            {
               [1,2,3,4].map((el,i) => <input ref={inp => this[`input_${i}`] = inp} key={i}/>)
            }
            <input type='submit'/>
         </form>
      )
   }
}

ReactDOM.render(<App/>, document.getElementById('app'));
&#13;
input{
   display: block;
   margin-bottom: 20px;
}
&#13;
<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='app'/>
&#13;
&#13;
&#13;

建议:

使用ref而不是使用多个controlled component,并将数据存储在状态变量We should avoid the use of ref中。