简体   繁体   English

从多个下拉列表中选择不同的值具有相同的数据

[英]Select different values from multiple dropdown lists have same data

Right now when I select an option from first selectlist, it automatically set the 2nd select list value, but I want if I select an option from first select list it should be set the value of first select and same for 2nd select list.现在,当我从第一个选择列表中选择一个选项时,它会自动设置第二个选择列表的值,但是我想如果我从第一个选择列表中选择一个选项,它应该设置第一个选择的值,并且第二个选择列表的值相同。

  import React, {useState} from "react";

    const options = [
    {
        label: "Acai Berry",
        value: "acaiberry",
        color: "#FF4D4D",
        quantity: 1,
        price: 20
    },

    {
        label: "Blood Orange",
        value: "bloodorange",
        color: "#FF4D4D",
        quantity: 1,
        price: 32
    },

    {
        label: "Honey Ginger Lemon",
        value: "honeyginger",
        quantity: 1,
        color: "#FF4D4D",
        price: 17
    }
    ];

    const RandomizerComponent = () => {
    const [bundle, setBundle] = useState("acaiberry");

    const handleChange = (e) => {
        setBundle(e.target.value);
    };
    const spin = () => {
        randomItems();
    };

    const randomItems = () => {
        const length = selectItems.length;
        const randomIndex = Math.floor(Math.random() * length);
        const data = selectItems[randomIndex].value;
        setBundle(data);
    };

    const quantity = 2;
    // Rabdomizer Qunatity and Data
    const quantityOfSomething = new Array(quantity).fill(options);

    return (
        <div>
        <h1 style={{textAlign: "center"}}>CONFIRM SELECTION</h1>
        <div className="select-container">
            <div className="select-Wrapper">
            {quantityOfSomething.map((item, index) => (
                <div key={`${item}-${index}`}>
                <select
                    value={bundle}
                    onChange={handleChange}
                    className="selectList"
                >
                    {item.map((option, index) => (
                    <option key={index} value={option.value}>
                        {option.label}
                    </option>
                    ))}
                </select>
                <div>
                    {item
                    .filter((option) => option.value == bundle)
                    .map((filteredBundle, index) => (
                        <div className="image-wrapper" key={index}>
                        <img src={filteredBundle.image} alt="" />
                        </div>
                    ))}
                </div>
                </div>
            ))}
            </div>
            <div>
            <button onClick={spin} className="spin-btn">
                Spin
            </button>
            </div>
        </div>
        </div>
    );
    };

    export default Randomizer;

Here is the output of above code这是上面代码的输出

在此处输入图片说明

Here is the reference website: https://simulate.com/buy/这是参考网站: https : //simulate.com/buy/

Your this code:你的这个代码:

<select value={bundle} onChange={handleChange} className="selectList" >

It is running through map but in both the select boxes you assign value={bundle}.它通过地图运行,但在两个选择框中都分配了 value={bundle}。 Therefore whenever you setBundle , it will impact states of both the select boxes.因此,无论何时setBundle ,它都会影响两个选择框的状态。

Kindly assign individual ids and check which selectbox was changed or create two separate change handlers and bind events to them separately.请分配单独的 id 并检查哪个选择框已更改或创建两个单独的更改处理程序并将事件分别绑定到它们。

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

相关问题 从 React 中的同一个选择下拉表单字段中获取多个值 - Get multiple values from the same select dropdown form field in React 从react js中的多个选择下拉列表中获取相同的值 - get same value from multiple select dropdown in react js 从选择下拉列表添加值反应 - Adding values from select dropdown react 同一页面上的多个react下拉列表为每个下拉列表获取相同的值 - Multiple react dropdowns on the same page are getting the same values for each dropdown React - 使用来自 API 的数据填充选择下拉列表 - React - Fill select dropdown with data from API 在React js中使用Reactstrap从多个选择下拉列表中动态选择选项 - Dynamically Select Options From Multiple Select Dropdown With Reactstrap In React js 在同一图表中使用具有不同值的多个数据集 - Using multiple dataset in the same chart with different values 在 React Admin 中从同一端点创建多个资源以应用不同的过滤器 - Create multiple resources from the same endpoint in React Admin to have different filters applied ReactJS - 即使从选择下拉列表中选择了相同的选项,也会触发事件 - ReactJS - trigger event even if the same option is selected from select dropdown 如何将值从下拉列表传递到选择的输入? 响应式JS - How to pass values from dropdown to inputs on select ? ReactJS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM