简体   繁体   English

在动态输入字段中处理 select 标签的 state 反应

[英]Handling state of select tag in dynamic input field react

I do not understand why the state is registered yet it does not have an effect to the intended value of beds.我不明白为什么注册了 state 但它对床的预期价值没有影响。 Here is the main component这是主要组件

const options = ["1", "2", "3"];
class Apps extends React.Component {  
  constructor(props) {    
    super(props)
    this.state = { 
       formValues: [{ name: "", email : "" ,beds:[options[0]]}]
     };
    
  }  
  handleChange(i, e) {
    let formValues = this.state.formValues;
    formValues[i][e.target.name] = e.target.value;
    this.setState({ formValues });
  }
  render() {
    const {formValues} = this.state;
    return (
        <PropertyType options={options} values={formValues}  handleChange={this.handleChange.bind(this)} />
    );
  }
}

Here is the property type component.这是属性类型组件。 I do not understand why its not working i have tried other means if anyone is willing to take a look at the code please do so我不明白为什么它不起作用我尝试了其他方法如果有人愿意查看代码请这样做

function PropertyType({values,add,remove,options,handleChange,handleSubmit}) {

    return (
        <form  onSubmit={handleSubmit}>
          {values.map((element, index) => (
            <div className="form-inline" key={index}>
              <label>Name</label>
              <input type="text" name="name" value={element.name || ""} onChange={e => handleChange(index, e)} />
              <label>Email</label>
              <input type="text" name="email" value={element.email || ""} onChange={e => handleChange(index, e)} />
              <select 
                  value={element.beds} 
                  onChange={e => handleChange(index, e)}>
                    {options.map((value) => (
                      <option value={value} key={value}>
                        {value}
                      </option>
                    ))}
               </select>
              {
                index ? 
                  <button type="button"  className="button remove" onClick={() => remove(index)}>Remove</button> 
                : null
              }
            </div>
          ))}
          <div className="button-section">
              <button className="button add" type="button" onClick={() => add()}>Add</button>
              <button className="button submit" type="submit">Submit</button>
          </div>
      </form> );
}

在此处输入图像描述

Your select input has no name, so when you change your select value, it updates the state reference value with "" name.您的 select 输入没有名称,因此当您更改 select 值时,它会使用""名称更新 state 参考值。

Notice the name value below:请注意下面的名称值:

      <select  
                  name="beds"
                  value={element.beds} 
                  onChange={e => handleChange(index, e)}>
                    {options.map((value) => (
                      <option value={value} key={value}>
                        {value}
                      </option>
                    ))}
               </select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM