简体   繁体   English

处理动态创建的事件 <select>下拉选项

[英]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: 流程示例:

  • Parent passes in aStateKey (actual state key), an array to be used as dropdown values, and an onChange function to be used to update aStateKey 父级传入aStateKey(实际状态密钥),用作下拉值的数组以及用于更新aStateKey的onChange函数
  • The dropdown menu is created from the passed inarray 下拉菜单是从传递的数组中创建的
  • A dropdown item is selected 选择了一个下拉项
  • aStateKey (which was passed in as a prop) gets updated via the passed in onChange function. aStateKey(作为道具传入)通过传入的onChange函数进行更新。

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.nameevent.value获取所需的目标值。

instead use event.target.name and event.target.value 而是使用event.target.nameevent.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.

相关问题 无法将选项动态添加到选择下拉列表 - Cant Append options to a select dropdown dynamically 如何使用选项填充动态创建的选择 - How to populate a dynamically created select with options 从动态创建的“选择”表单中检索选项 - Retrieving options from a dynamically created Select form 将鼠标事件绑定到动态创建的选择 - Binding mouse events to dynamically created select 使用JavaScript从PHP数组中填充带有选项的动态创建的下拉列表 - Populate Dynamically Created Dropdown with Options from PHP array using javascript 在React js中使用Reactstrap从多个选择下拉列表中动态选择选项 - Dynamically Select Options From Multiple Select Dropdown With Reactstrap In React js 如何为在javascript函数中创建的下拉菜单提供多个选择选项 - how to give multiple select options for a dropdown created in javascript function 没有选项<select>从 JS 动态填充时的下拉列表 - No options in <select> dropdown when dynamically populated from JS 动态添加下拉列表,并基于一个下拉列表中的选择添加其他列表中的选项 - Adding dropdowns dynamically and based on selection in one dropdown select options in other 如何预先选择动态生成的下拉选项? - how to pre-select a dropdown options which are dynamically generating?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM