简体   繁体   English

我如何将数组从选择html元素组件传递到数组中具有不同值的两个文本组件

[英]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.

相关问题 如何从一个组件内的数组传递对象? - How do I pass object from array within one component? 如何将props传递到从组件数组映射的组件? - How to pass props to a component mapped from an array of components? 如何使用输入组件数组中的输入值创建数组? - How do I create an array with the input values from an array of input Components? 如何使每个角度分量具有不同的数组值 - How to make each angular component have a different array values 如何清除打字稿中数组中的元素值? - How do I clear element values from an array in typescript? 如何使用变量(B)中的值选择数组(A)中的变量 - How do I select variables in an array (A) with values from a variable (B) 如何将状态中定义的数组中的值传递到查看组件的组件中? - How do I pass a value from an array defined in state into a component which beholds a component? 如何从选择标签传递数组对象并将值传递给 API? - How to pass array object from select tag and pass values into API? 如何将 class 添加到 React 组件数组中的每个元素? - How do I add a class to each element in an array of React Components? 如果我的javascript数组的值包含两个元素,如何使用一个元素而不使用另一个元素? - If my javascript array has values that have two elements how can I use one element without the other?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM