【问题标题】:Reset multiselect drop-down menu react重置多选下拉菜单反应
【发布时间】:2020-09-05 15:28:28
【问题描述】:

我在表单中使用 multiselect-dropdown-react,我的问题是,如何重置多选字段,我的意思是带有选项的输入?因为我真的看过文档,找不到重置的方法,所以整个表单都重置了,除了多选。

const multiselectRef = useRef();

    function resetValues() {
   multiselectRef.resetSelectedValues();
  }

  <Multiselect
    options={props.categories}
    displayValue="name"
    onSelect={handlerOnChangeCategory}
    onRemove={handlerOnChangeCategory}
    ref={resetValues}
   />

谢谢,谢谢

【问题讨论】:

  • 你使用的是哪个库?
  • 我还是不明白这个问题。您想在有人点击某物时重置表单吗?
  • 表单通过简单的 onSubmit 重置,但多选不会。

标签: javascript html css reactjs function


【解决方案1】:

如果我理解正确,您想在某些操作上重置多选。你可以这样做:

import React, { useState, useRef } from "react";
import { Multiselect } from "multiselect-react-dropdown";

import "./styles.css";

export default function App() {
  const [options] = useState([
    { name: "Srigar", id: 1 },
    { name: "Sam", id: 2 }
  ]);
  const multiselectRef = useRef();

  const resetSelectField = () => {
    multiselectRef.current.resetSelectedValues();
  };

  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
      <Multiselect
        options={options} // Options to display in the dropdown
        displayValue="name" // Property name to display in the dropdown options
        ref={multiselectRef}
      />
      <button onClick={resetSelectField}>Reset</button>
    </div>
  );
}


这里是演示:https://codesandbox.io/s/infallible-sound-3ek6n?file=/src/App.js:0-786

【讨论】:

  • 非常感谢,我会尝试并通知您
  • 酷。如果这对您有用,请确保对此进行投票并标记已接受的答案。如果有人遇到同样的问题,就会弹出这个问题(stackoverflow 算法)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-21
相关资源
最近更新 更多