React从动态添加的输入文本字段中获取值
React get values from dynamically added inputs text fields
我创建了动态生成输入文本字段,但无法找到读取和获取值的方法,并将其存储到数组中。请在下面找到代码
我有一个单独的组件,用于添加名为IncrementTableRow的新输入字段行
import React, {PropTypes} from 'react';
class IncrementTableRow extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<tr>
<th scope="row">{this.props.index}</th>
<td><input type="text" className="form-control" ref={"firstValue"+this.props.index} placeholder=""/></td>
<td><input type="text" className="form-control" ref={"secondValue"+this.props.index} placeholder=""/></td>
</tr>
);
}
}
export default IncrementTableRow;
此外,我有一个主要组件要调用IncrementTableRow。下面是调用行。
export default class SuggestInterestProductDetails extends Component {
constructor(props) {
super(props);
this.state = {
rows: []
};
this.AddRow = this.AddRow.bind(this);
}
AddRow() {
this.setState({
rows: [{val: 5}, ...this.state.rows]
});
}
render() {
let rows = this.state.rows.map(row => {
return <Row />
});
return (
<div>
<button onClick={this.AddRow}>Add Row</button>
<table>
{rows}
</table>
</div>
);
}
}
我需要读取每个生成的文本字段值,并将其存储到数组
您的代码示例似乎不完整——您甚至没有将值添加到行中所以这里只有一个简短的答案:
检查反应参考https://facebook.github.io/react/docs/more-about-refs.html
您可以为中的每一行添加一个引用
let rows = this.state.rows.map(row => {
return <Row />
});
也许一个更好的解决方案是在您的行中添加一个onChange侦听器,并更新您的parrent组件的状态
let rows = this.state.rows.map((row,i) => {
return <Row ref={'row-'+i} onChange={(event) => this.myListener(event,i)} />
});
相关文章:
- 从 jquery 动态输入字段获取值
- 角度形式 - 从包装器中的字段获取模型
- 使用Javascript从html字段获取年龄
- 通过ACF从WordPress中的重复字段获取Brightcove视频
- 按标签时间字段获取字段值,如下所示
- 如何在不提交表单的情况下从输入字段获取文件路径
- 使用jQuery从隐藏输入字段获取值的问题
- 如何从输入字段获取多个值并添加到文本区域
- 通过数组中的一个字段获取对象
- PHP和如何在点击连接到每个输入字段的按钮后从多个输入字段获取值
- 从输入字段获取图像信息
- 从隐藏字段获取值- JavaScript
- 通过php从innerhtml字段获取值
- 从多个文件上传字段获取文件名
- 在MVC中通过Jquery从隐藏字段获取多个值
- 从输入字段获取变量值,并在SELECT-WHERE语句中使用它
- Javascript:按特定字段获取所有对象
- 是否可以使用javascript从文件字段获取$_FILES["inputID"]["tm
- 动态 crm - 使用 JavaScript 从 CRM 中的日期和时间字段获取日期字符串值
- 如何使用angular.copy从一个输入字段获取值到另一个输入字段?