简体   繁体   English

如何从 select 中获取两个不同的值?

[英]How to get two different values from a select?

I need to split a string and store the pieces in different var.我需要拆分一个字符串并将这些片段存储在不同的 var 中。

I have an array called referti我有一个名为referti的数组

[ {referti.hash_referto}, {referti.data_esame}, {referti.tipo_esame}] 

for a list of elements.对于元素列表。 I display those elements in a select by mapping like this我通过这样的映射在 select 中显示这些元素

render() {
    const refertiItems = this.state.referti.map((referti, i) => {
        return (
            <option key={referti.hash_referto}>
              {referti.tipo_esame}-{referti.data_esame} {referti.hash_referto}
            </option>
        )
    });

    return(
        <Label for="type" text="Seleziona un referto" />
        <select
            name="codiceReferto"
            placeholder="Selezionare Referto"
            onKeyPress={this.onEnter}
            value={this.codiceReferto}
            onChange={this.handleInputChange}
        >
            <option default value="vuoto" />
            {refertiItems}
        </select>
    );
}

then when the user choose one element of the select I save the value in nameArray[ ] initialized in the state.然后当用户选择 select 的一个元素时,我将值保存在 nameArray[] 中,该值在 state 中初始化。

handleInputChange() {
    console.log("valore", this.codiceReferto)
    const target = event.target;
    const nameArray = this.state


    const value =
      target.type === 'checkbox'
        ? target.checked
        : target.value;
    const name = target.name;

    if (name === "codiceReferto") {
      const newArray = value.split(" ")
      this.setState({ nameArray: newArray})
    }
}

so now the nameArray is: [0]: tipo_esame and data_esame [1]: hash_referto所以现在 nameArray 是:[0]:tipo_esame 和 data_esame [1]:hash_referto

How can i store nameArray[0] in descrizioneReferto initialized in the state and nameArray[1] in codiceReferto initialized in the state?我如何在 state 中初始化的 decrizioneReferto 中存储 nameArray[0],在 state 中初始化的 codiceReferto 中存储 nameArray[1]?

I think you have 2 options here.我认为您在这里有两个选择。

  1. You can directly set values fom newArray ,您可以直接从newArray设置值,
if (name === "codiceReferto") {
   const newArray = value.split(" ")
   this.setState({ 
      nameArray: newArray,
      descrizioneReferto: newArray[0],
      codiceReferto: newArray[1] 
   })
}
  1. You can use callback in setState to get and store value's from nameArray您可以在 setState 中使用回调从nameArray获取和存储值
if (name === "codiceReferto") {
   const newArray = value.split(" ")
   this.setState({ 
      nameArray: newArray,  
   }, () => this.setState({
      //here you can access `nameArray`
      descrizioneReferto: this.state.nameArray[0],  
      codiceReferto: this.state.nameArray[1] 
   }))
}

In you question I don't understand what do you want to do.在你的问题中,我不明白你想做什么。 But I created a working example.但我创建了一个工作示例。 In comments, please provide more info.请在评论中提供更多信息。

Code:代码:

import React, { Component } from "react";
import ReactDOM from "react-dom";

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      referti: [
        {
          tipo_esame: 1,
          data_esame: "referto",
          hash_referto: "aaa"
        },
        {
          tipo_esame: 2,
          data_esame: "referto",
          hash_referto: "bbb"
        },
        {
          tipo_esame: 3,
          data_esame: "referto",
          hash_referto: "ccc"
        },
        {
          tipo_esame: 4,
          data_esame: "referto",
          hash_referto: "ddd"
        }
      ],
      nameArray: ["Andrei", "Fiodorov", "Mike", "Rug"],
      descrizioneReferto: null,
      codiceReferto: null
    };
  }

  handleChange = select => {
    console.log(select.target.value);
    const { nameArray } = this.state;
    this.setState({
      descrizioneReferto: nameArray[0],
      codiceReferto: nameArray[1]
    });
  };

  render() {
    const { referti } = this.state;

    return (
      <div className="App">
        <h1>Help me!</h1>
        <select onChange={el => this.handleChange(el)}>
          {referti.map((item, index) => {
            return (
              <option value={index}>
                {item.tipo_esame}-{item.data_esame} {item.hash_referto}
              </option>
            );
          })}
        </select>
      </div>
    );
  }
}

ReactDOM.render(<App />, document.getElementById("root"));

Codesandbox example:代码沙盒示例:

编辑 nifty-hawking-mg8ch

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

相关问题 如何从单选中获取两个值 - how to get two values from single select 如何获取两个不同 select 元素的值并在 function 中使用它们 - How to get values of two different select elements and use them in a function 如何从jQuery中两个不同的可排序值中获取值 - how to get the values from two different sortable values in jquery 如何获得两个不同的 select 框值并根据香草 JS 中的值组合显示结果? - How to get two different select box values and show a result based on combination of values in vanilla JS? ddslike从差异选择中获取两个值 - ddslike get two values from diffrentes select 一次从选择选项中获取两个值 - Get two values from select option at once 我如何从具有相同类的不同选择类型中选择值并获取其值 - How can i select values from different select types having same class and get their value 如何使用两个不同集合中的值创建if循环 <select>元素 - How to create if loops using values from two different sets <select> elements 如何比较下拉/选择菜单中的两个不同值,并将其作为一个值应用于条件语句? - How do I compare two different values from a dropdown/select menu and apply it to a conditional statement as one value? 如何计算不同选择的期权价值 - How to Calculate Values of Options from Different Select
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM