[英]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.我认为您在这里有两个选择。
newArray
,您可以直接从newArray
设置值,if (name === "codiceReferto") {
const newArray = value.split(" ")
this.setState({
nameArray: newArray,
descrizioneReferto: newArray[0],
codiceReferto: newArray[1]
})
}
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:代码沙盒示例:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.