首页 > 解决方案 > 如何在antD select onChange中传递第二个参数?

问题描述

  1. 我想在 onchange 中访问 select 的 id。我正在尝试类似下面的东西。根据文档,第二个参数需要是对象

  2. 还有如何通过单击删除来删除这个特定的选择下拉菜单

 var arr = [];
        var x = NameCount
        while (x > 0) {
          console.log(x)
           arr.push(
            <div style={{width:"100%" , minWidth:'5px', marginRight:'0.5rem'}} >
              <Select id={x-1} style={{ width: "100%" , minWidth:'5px'}} placeholder="Group name" onChange={(e,{key:id})=>handleGroupByname(e,{key:id})}>
                {dropdown_values.map((y) => (
                  <Option key={y}>{y}</Option>
                ))}
              </Select>
             <span onClick={handleDelete}>delete<span>
            </div>
          );
          x--;
        }
        return arr;

标签: reactjsantd

解决方案


对于 onchange,您可以执行以下操作:

const handleGroupByname = (value, selectId) => {
  // do your stuff here
}

...
...

<Select id={x-1} onChange={(value)=>handleGroupByname(value, x-1)}>
...
...
</Select>

对于删除,您可以应用相同的想法

const handleDelete = (e, id) => {
  // find the id and
  // remove the item from you arr
}

<span onClick={(e) => handleDelete(e, x-1)}>delete<span>


推荐阅读