简体   繁体   English

从react js中的多个选择下拉列表中获取相同的值

[英]get same value from multiple select dropdown in react js

I won't do to like this.我不会喜欢这个的。 I have a basket and here I have five apples.我有一个篮子,这里有五个苹果。 I want to divide it into five-person.我想把它分成五个人。 If a user takes 2 items from here.如果用户从这里拿走 2 件物品。 Then the next person will have to be available 3 items.然后下一个人必须有 3 件物品。 If this person takes 2 items then the basket has only 1 item.如果这个人拿了 2 件物品,那么篮子里只有 1 件物品。 The five-user will be like五用户将像

  <select>
    <label>userOne</label>
    <option>0</option>
    <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    <option>5</option>
  </select>
  {/* userOne selected 2 */}
  <select>
    <label>userTwo</label>
    <option>0</option>
    <option>1</option>
    <option>2</option>
    <option>3</option>
    {/* removed automaticaly 4 and five option */}
    {/* <option>4</option>
    <option>5</option> */}
  </select>
  {/* userTwo select 2 */}
  <select>
    <label>userThree</label>
    <option>0</option>
    <option>1</option>
    {/* <option>2</option>
    <option>3</option> */}
    {/* 3-2 = 1 */}
  </select>
  {/* userFour have only one  */}
  <select>
    <label>userFour</label>
    <option>0</option>
    <option>1</option>
    {/* <option>2</option>
    <option>3</option>
    <option>4</option>
    <option>5</option> */}
  </select>
  {/* userFour already selected one */}
  <select>
    <label>userFive</label>
    <option>0</option>
    {/* userFive have no item available */}
    {/* <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    <option>5</option> */}
  </select>

As far as I understand, you want to filter the list of options depending on the selected data据我了解,您希望根据所选数据过滤选项列表
For this you need to display options dynamically.为此,您需要动态显示选项。
Example of such code:此类代码的示例:

import React, { useState } from "react";

const USER_COUNT = 5;
const OPTION_COUNT = 5;
const options = Array(OPTION_COUNT + 1)
  .fill(0)
  .map((_, i) => i);

function App() {
  let [users, setUsers] = useState(Array(USER_COUNT).fill(0));

  const handleChange = (idx) => (e) => {
    setUsers(
      users.map((user, userIdx) => {
        if (userIdx < idx) {
          return user;
        }
        if (userIdx === idx) {
          return +e.target.value;
        }
        return 0;
      })
    );
  };

  return (
    <div className="App">
      {users.map((user, userIdx) => {
        let prevOptionsSum = 0;
        for (let i = 0; i < userIdx; i++) {
          prevOptionsSum += users[i];
        }
        let userOptions = options.slice(0, options.length - prevOptionsSum);
        return (
          <div key={userIdx}>
            <label>user {userIdx + 1}</label>
            <select value={user} onChange={handleChange(userIdx)}>
              {userOptions.map((_, userOptionIdx) => (
                <option key={userOptionIdx}>{userOptionIdx}</option>
              ))}
            </select>
          </div>
        );
      })}
    </div>
  );
}
export default App;

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

相关问题 从中获取价值<select>React JS 中的(下拉) - Get value from <select> (dropdown) in React JS 从 React 中的同一个选择下拉表单字段中获取多个值 - Get multiple values from the same select dropdown form field in React 在React js中使用Reactstrap从多个选择下拉列表中动态选择选项 - Dynamically Select Options From Multiple Select Dropdown With Reactstrap In React js 从 API 填充的 select 下拉列表中反应获取值 - React get value from select dropdown populated from API 如何 select 反应 js 中的多个下拉值我正在使用`react-select` - How to select multiple dropdown value in react js i am using `react-select` 如何从反应中的材料选择下拉列表中获取值 - How to get the value from the material select dropdown in react 无法从 react-select AsyncSelect 下拉列表中获取选定的值 - Unable to get selected value from react-select AsyncSelect dropdown 具有唯一选择的多选下拉菜单-React JS - Multiple select dropdown with unique selection - React JS 无法使用React JS从Materialize下拉列表中获得选定的值 - Cannot get the Selected Value from Materialize dropdown with React JS 使用React获取文本而不是下拉列表(Select)输入的值 - Get Text not Value of Dropdown (Select) input with React
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM