简体   繁体   English

如何在反应 js 中获得多个 select 选项?

[英]How can i get multiple select options in react js.?

So basically iam new in react and iam trying to create multiple select option using axio get method i have a problem that how can i add multiple select option in this file iam trying to do this with check box code below but keep getting error that a string is called on change function.所以基本上我是新的反应和我试图创建多个 select 选项使用 axio get 方法我有一个问题,我如何在这个文件中添加多个 select 选项我试图用下面的复选框代码做到这一点,但不断收到错误的字符串在更改 function 时调用。 Other than that the checkboxes are not opening due to that function除此之外,由于 function 复选框没有打开

List item项目清单

import React, { Component, Fragment } from "react";
import axios from "axios";

class Home extends Component {
  state = {
    users: []
  };

  componentDidMount() {
    axios.get("https://jsonplaceholder.typicode.com/users").then(res => {
      console.log(res);
      this.setState({
        users: res.data
      });
    });
  }

  showCheckboxes = () => {
    let expanded = false;
    const checkboxes = document.getElementById("checkboxes");
    if (!expanded) {
      checkboxes.style.display = "block";
      expanded = true;
    } else {
      checkboxes.style.display = "none";
      expanded = false;
    }
  };

  onChangeValue = e => {
    const value = e.target.value;
    debugger;
  };

  render() {
    const { users } = this.state;

    const nameList = users.length ? (
      <select className="custom-select">
        {users.map((user, i) => {
          return (
            <option key={i} value={user.name}>
              {" "}
              {user.name}
            </option>
          );
        })}
      </select>
    ) : (
      "No Data"
    );

    const usernameList = users.length ? (
      <select className="custom-select">
        {users.map((user, i) => {
          return (
            <option key={i} value={user.username}>
              {user.username}
            </option>
          );
        })}
      </select>
    ) : (
      "No Data"
    );

    const emailList = users.length ? (
      <select className="custom-select">
        {users.map((user, i) => {
          return (
            <option key={i} value={user.email}>
              {user.email}
            </option>
          );
        })}
      </select>
    ) : (
      "No Data"
    );

    return (
      <Fragment>
        {nameList}
        <hr />
        {usernameList}
        <hr />
        {emailList}
        <hr />

        <div className="multiselect">
          <div className="selectBox">
            <select>
              <option>Select an option</option>
            </select>
            <div className="overSelect" onClick="showCheckboxes()"></div>
          </div>
          <div id="checkboxes">
            <label htmlFor="one">
              <input type="checkbox" id="one" />
              First checkbox
            </label>
            <label htmlFor="two">
              <input type="checkbox" id="two" />
              Second checkbox
            </label>
            <label htmlFor="three">
              <input type="checkbox" id="three" />
              Third checkbox
            </label>
          </div>
        </div>
      </Fragment>
    );
  }
}

export default Home; 

this line:这一行:

<div className="overSelect" onClick="showCheckboxes()"></div>

to

<div className="overSelect" onClick={this.showCheckboxes}></div>

You can use this open-source component from GitHub: https://github.com/JedWatson/react-select .您可以使用 GitHub 中的这个开源组件: https://github.com/JedWatson/react-select

Look into this article its really well structured demonstration what you need.看看这篇文章,它的结构非常好,演示了你需要什么。 how to get multiple select options in reactjs 如何在 reactjs 中获得多个 select 选项

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

相关问题 反应JS。 如何在对象内部显示图像? - React JS. How can I display the image inside the object? 我想导出在 React Js 中的 function 组件中声明的变量。 我怎样才能做到这一点? - I want to export variable that declared inside the function component in React Js. How can I do that? 如何根据react js中的id依次显示一个api的数据? - How can i display data of an api one after another on the basis of id in react js.? 如何在 react js 的表格网格中显示键值对? - How can i display key value pairs in a table grid in react js.? 如何使用Vue js创建收藏夹列表。 允许用户选择最多5个选项 - How to create a favorite list with Vue js. Allowing the user to select maximun 5 options 我如何 select 反应 js 中的多个值? - How can i select multiple values in react js? js。 使用 id 获取多个值 - js. get multiple values using id 从React中的多个选择表单中获取选项 - Get options from multiple select form in React 文件上传-Node Js(Express)到React Js。 如何获得正确的图像路径? - File upload - Node Js(Express) to React Js. How to get correct image path? 如何为React-Select添加更多可搜索的选项? - How can i add more searchable options to React-Select?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM