[英]Handling events on dynamically created <select> dropdown options
Let's say I have a function which returns a dropdown. 假设我有一个返回下拉菜单的函数。 This function gets called from some parent, where the parent passes in props including a state key, array, and onChange.
该函数从某个父对象调用,该父对象在其中传递道具,包括状态键,数组和onChange。 The dropdown is created dynamically from the items in the array.
下拉列表是根据数组中的项目动态创建的。 What I want to happen is, when an option in the dropdown is selected, the parents passed in state key gets updated to the value of whatever was selected.
我想发生的是,当选择下拉菜单中的一个选项时,传递给状态键的父项将更新为所选值。 Currently, I am trying to do this by setting an onClick handler per , which doesn't work.
目前,我正在尝试通过设置一个onClick处理程序来实现此目的,该方法不起作用。 Instead, I am met with either no or undefined values (described more below).
取而代之的是,我遇到了没有值或未定义的值(下面将详细介绍)。
Example flow: 流程示例:
I understand that the traditional method is to give and onChange handler, but I am having troubles working out how to get the desired described above. 我知道传统的方法是给定和onChange处理程序,但是我在解决如何获得上述期望时遇到了麻烦。
Parent 父级
state = { aStateKey: "" };
someArray = ["test", "another test"];
updateField = (name, value) => {
console.log("Updating field: " + name + " with value: " + value);
}
return(
<div>
<CreateDropdown name="aStateKey" items={this.someArray} onChange={this.updateField} />
</div>
);
CreateDropdown CreateDropdown
function CreateDropdown(props) {
const handleClick = event => {
console.log("changed name:" + event.name + "changed value: " + event.value);
props.onChange(event.name, event.value);
};
return (
<div>
<select>
{props.items.map(field => (
<option key={field} value={field} name={props.name} onClick={handleClick}>
{field}
</option>
))}
</select>
</div>
);
}
Console log 控制台日志
Shows nothing! 什么也没显示! However, if I move the onClick from
<option>
to <select>
, ie 但是,如果我将onClick从
<option>
移到<select>
,即
return (
<div>
<select onChange={handleClick}>
{props.items.map(field => (
<option key={field} value={field} name={props.name}>
{field}
</option>
))}
</select>
</div>
);
The console shows: 控制台显示:
Updating field: undefined with value: undefined.
更新字段:未定义,值:未定义。
changed name:undefinedchanged value: undefined
更改名称:未定义更改值:未定义
How can I achieve my desired behavior? 我怎样才能达到我期望的行为?
your target form this event is select and use onChange and here the updated function you need: 您的目标表单是选择此事件并使用onChange,这里需要使用更新的功能:
function CreateDropdown(props) {
return (
<div>
<select name={props.name} onChange={e =>
props.onChange(e.target.name, e.target.value);}>
{props.items.map(field => (
<option key={field} value={field}
{field}
</option>
))}
</select>
</div>
);
}
UPDATE #1 更新1
Inside handleClick
, you are using event.name
and event.value
to get the target values you want. 在
handleClick
内部,您正在使用event.name
和event.value
获取所需的目标值。
instead use event.target.name
and event.target.value
而是使用
event.target.name
和event.target.value
Try using onChange
instead of onClick
in your select
element. 尝试在
select
元素中使用onChange
而不是onClick
。
It belongs to select
not option
elements. 它属于
select
而非option
元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.