我试图在选择一个值之后从select元素中获取价格值。例如,股票说明:“ Numbers Invo Qt”,售价:“ 100”。但是我已经编写了下面的代码,当我运行它时,出现此错误Selling-Price返回“ undefined”。
因此,我非常感谢您能为我提供帮助,因为我整天都在互联网上寻找帮助。
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>
);
}
}
答案 0 :(得分:0)
价格不会像这样过去。我的建议是,要onChange传递整个对象。
onChange={ (e) => props.handleSelect(props.items[e.target.value]) }