有没有一种方法可以为Semantic UI React Dropdown提供html“ name”属性,使其表现得像表单提交中的普通输入元素?

保罗·桑德

对于普通的html元素,您可以通过表单获取该元素的值。

 function handleSubmit(event) {
    event.preventDefault()
    const formData = new FormData(e.target)
    console.log(formData.get("test"))
    // logs the value of the html element with the name "test"
  }

<form onSubmit={handleSubmit}>
    <input name="test" />
    <button type="submit">Submit</button>
</form>

表单将为键“测试”分配表单提交中输入内容的值。

我的问题:有没有办法使此功能与语义UI下拉菜单一起使用?

理想情况下,下面是我认为可以使用的内容,但我不知道,因为该组件是其他元素的包装。

 function handleSubmit(event) {
    event.preventDefault()
    const formData = new FormData(e.target)
    console.log(formData.get("test"))
    // logs the value of the html element with the name "test"
  }

<form onSubmit={handleSubmit}>
    <Dropdown name="test" selection options={
        [{key: '1', text: 'text', value: '1'}]
      }/>
    <button type="submit">Submit</button>
</form>

在提交表单时,将在该下拉列表中选择的任何值放入formData中名为“ test”的键中。如果未选择任何值,则将未定义的值赋予键“ test”。

这可能吗?我还有一个使用基本html选择器的方法,但是想不更改很多已经编写的代码。

诺亚·甘约

是的,但是您需要解构下拉菜单组件。

const [open, setOpen] = useState(false)

<Dropdown
    trigger={<Button onClick={() => setOpen(true)}>Open</Button>}
    onClose={() => setOpen(false)}
    icon={null}
    open={open}
    simple
  >
    <Dropdown.Menu>
      ... add the options with the "name" tag ...
    </Dropdown.Menu>
  </Dropdown>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

Related 相关文章

热门标签

归档