简体   繁体   English

如何通过材料ui在自动完成时获取输入框值而不是选定的li标签?

[英]How to get the input box value not the selected li tag on autocomplete by material ui?

How to get the value of inputbox on the autocomplete?如何在自动完成上获取输入框的值? Because if I used the e.target or e.currentTarget using onchange it will display the li tag not the inputbox.因为如果我使用 e.target 或 e.currentTarget 使用 onchange 它将显示 li 标签而不是输入框。

Inputbox输入框

在此处输入图像描述

Console output控制台 output

在此处输入图像描述

Code
const handleChangeAutoComplete = (e, val) => {
    console.log(e.target);
  };


<Autocomplete
                  className={taskClasses.SelectAssignee}
                  options={users}
                  value={users.fullname}
                  onChange={handleChangeAutoComplete}
                  getOptionLabel={(user) => user.fullname}
                  renderInput={(params) => (
                    <TextField
                      placeholder='Assignee'
                      value={params.fullname}
                      {...params}
                      variant='outlined'
                    />
                  )}
                />

You will get user input in userInput state,You should get val second argument that you have passed to get object of selected value and instead of using onChange which listens for user select change, you need to use onInputChange, it will send what user types您将在 userInput state 中获得用户输入,您应该获得您传递的val第二个参数以获取 object 的选定值,而不是使用监听用户 Z99938282F04071859941E18F16EFCF4 的 onChange 用户类型更改,您需要发送什么,使用 onInputChange2


const handleChangeAutoComplete = (e, val) => {
    console.log(val,"your value will be stored here inside second argument");
  };

Complete Code on how to handle autocomplete关于如何处理自动完成的完整代码


/* eslint-disable no-use-before-define */
/* eslint-disable no-use-before-define */
import React, { useState } from "react";
import TextField from "@material-ui/core/TextField";
import Autocomplete from "@material-ui/lab/Autocomplete";

export default function ComboBox() {
  const [userInput, setUserInput] = useState("");
  const [option, setOption] = useState("");
  return (
    <Autocomplete
      id="combo-box-demo"
      options={top10Films}
      getOptionLabel={(option) => option.title}
      style={{ width: 300 }}
      value={option}
      onChange={(e, v) => {
        setOption(v);
      }}
      onInputChange={(e, v) => {
        setUserInput(v);
      }}
      renderInput={(params) => (
        <TextField {...params} label="Combo box" variant="outlined" />
      )}
    />
  );
}

// Top 100 films as rated by IMDb users. http://www.imdb.com/chart/top
const top10Films = [
  { title: "The Shawshank Redemption", year: 1994 },
  { title: "The Godfather", year: 1972 },
  { title: "The Godfather: Part II", year: 1974 },
  { title: "The Dark Knight", year: 2008 },
  { title: "12 Angry Men", year: 1957 },
  { title: "Schindler's List", year: 1993 },
  { title: "Pulp Fiction", year: 1994 },
  { title: "The Lord of the Rings: The Return of the King", year: 2003 },
  { title: "The Good, the Bad and the Ugly", year: 1966 },
  { title: "Fight Club", year: 1999 }
];


You can refer to this sandbox你可以参考这个沙盒

编辑材质演示(分叉)

onInputChange= {(event, value)=>{
  console.log(event)
  console.log(value)
}}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM