首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何输入名称price1、price2、price3....在react中

如何输入名称price1、price2、price3....在react中
EN

Stack Overflow用户
提问于 2017-01-10 15:13:04
回答 2查看 35关注 0票数 0

代码语言:javascript
复制
let arrNum = 0;

function addInput() {
  // click add input btn
  arrNum += 1;
}

<input type="text" name={ "price" + arrNum } />
<button ... onClick= { this.addInput }></button>

每次用户按下一个按钮,添加输入和arrNum += 1。

当我点击3次点击时我想要什么

代码语言:javascript
复制
<input type="text" name="price1" />
<input type="text" name="price2" />
<input type="text" name="price3" />

但我点击

代码语言:javascript
复制
<input type="text" name="price3" />
<input type="text" name="price3" />
<input type="text" name="price3" />

对不起,我的英语不好

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-01-10 16:41:44

您可以尝试这样做。

代码语言:javascript
复制
const addComponents = (num) => {
    let i;
    for(i = 0; i < num; i++) {
        const priceName = 'price'+i;
        arr.push(<AddForm priceName= {priceName}/>); //price name will change for each loop price0, price1, price2 ...
    }

    return arr;
  }

在addForm中

代码语言:javascript
复制
 this.state = {
    price: this.props.priceName,
    largeValue: '',
    middleValue: '',
    smallValue: '',
    seValue: '',
    seseValue: '',

    largecat: la1
  };
票数 0
EN

Stack Overflow用户

发布于 2017-01-10 15:53:55

我会将您的输入添加到state中的一个数组中。

代码语言:javascript
复制
constructor(props, context) {
  super(props, context);
  this.state = {
    inputs: ["price1"],
  };
}

然后,您可以执行以下操作:

代码语言:javascript
复制
render() {
  return (
    {this.state.inputs.map(
      (inputName, i) => <input type="text" name={inputName} key={i} />
    )}
    <button ... onClick= { this.addInput }></button>
  );
}

function addInput() {
  let temp = this.state.inputs.slice();
  let size = this.state.inputs.length+1;
  this.setState({inputs: temp.push("price"+length)});
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41563140

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档