首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >React native select:访问所选选项的键/索引

React native select:访问所选选项的键/索引
EN

Stack Overflow用户
提问于 2019-01-16 19:58:36
回答 1查看 404关注 0票数 0

我将这个原生select实现为一个dropdown组件。我可以用e.target.value访问选定的值。如何访问映射选项的键值或索引:

代码语言:javascript
复制
<div className={`dropdown ${className} ${isInline ? "dropdown--inline" : ""}`}
style={{ width: width + "px", backgroundColor: styles!.dropdownBackgroundColor, borderColor: styles!.borderColor }}
                    >
    <select className="dropdown__portable-select"
         onChange={e => this.selectItem(e.target.value, true, e.target.index)} value={JSON.stringify(selectedValue)} // This line
                        >
    {selectedValue === unselectedValue && unselectedValue !== undefined ?
       <option value="">{unselectedValue}</option>
       : null}
    {options.map((o: any, index: number) => (
      <option key={index} value={JSON.stringify(o)}>{this.renderValue(o)}</option>
    ))}
  </select>
  <div className="dropdown__spacer" style={{ backgroundColor: styles!.borderColor }} />
  <div className="dropdown__icon-container" style={{ color: styles!.iconColor }}>
    <SvgIcon className="dropdown__icon" iconName="material-design/arrow_down_thin" />
  </div>
</div>

这个问题特别是这一行:

代码语言:javascript
复制
onChange={e => this.selectItem(e.target.value, true, e.target.index)} value={JSON.stringify(selectedValue)}

e.target.index显然不存在。在映射函数中使用e.target.key来寻址索引{key=}也是如此。

如何访问select上的key或index属性?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-16 20:05:56

您可以在select元素上访问名为selectedIndex的属性。

代码语言:javascript
复制
this.selectItem(e.target.value, true, e.target.selectedIndex)

您不能访问key,因为它是由React管理的特殊属性。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54216613

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档