[英]how do i pass array from select html element component to two text components that have different values in the array
I'm trying to get the price value from the select element after I've selected a value. 选择值后,我试图从select元素中获取价格值。 eg stock-description:"Numbers Invo Qt", Selling-Price:"100".
例如,股票说明:“ Numbers Invo Qt”,销售价格:“ 100”。 But I've written the code below and when i run it i get this error Selling-Price returns "undefined".
但是我已经编写了下面的代码,当我运行它时,出现此错误Selling-Price返回“ undefined”。
So I would really appreciate it you could help me because I've search the internet the whole day looking for help. 因此,我非常感谢您能为我提供帮助,因为我整天都在互联网上寻找帮助。
import React, { Component } from 'react';
import _ from 'lodash';
const ITEMS = [
{ name: 'initial', text: 'Select Items', value: '' },
{ name: 'centos', text: 'Invo qt', value: 'Numbers Invo qt', price: "100" },
{ name: 'ubuntu', text: 'Invo xp', value: 'Numbers Invo xp', price: "180" },
]
const SelectComponent = (props) => (
<select name={props.name}
onChange={props.handleSelect}
>
{_.map(props.items, (item, i) =>
<Option
key={i}
name={item.name}
value={item.value}
text={item.text}
price={item.price}
handleSelect={props.handleSelect}
/>
)}
</select>
);
const Option = (props) => (
<option
value={props.value}
>{props.text}</option>
)
export default class CustomerComboBox extends Component {
constructor() {
super()
this.state = {
selected: '',
selected_Price: ''
}
this.handleSelect = this.handleSelect.bind(this);
}
handleSelect(e) {
e.preventDefault();
//console.log(e.target);
this.setState({
selected: e.target.value,
selected_Price: e.target.price,
})
document.getElementById('stock-description').value = e.target.value;
document.getElementById('stock-price').value = e.target.price;
}
render() {
return (
<div>
<div>
<SelectComponent
name="testSelect"
items={ITEMS}
price={ITEMS.price}
handleSelect={this.handleSelect}
/>
</div>
<p>Stock Description</p>
<input id='stock-description' placeholder='Item Description'></input>
<p>Stock Price</p>
<input id='stock-price' placeholder='Selling Price'></input>
</div>
);
}
}
Price won't get passed like that. 价格不会像那样过去。 My advice would be, to onChange pass the entire object.
我的建议是,要onChange传递整个对象。
onChange={ (e) => props.handleSelect(props.items[e.target.value]) }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.