【发布时间】:2017-08-27 05:41:29
【问题描述】:
countries()
{
return [{"name":"Afghanistan","code":"AF"},{"name":"Albania","code":"AL"},...];
}
handleCountryChange(e)
{
console.log('NAME -> ' + e.target.value, ', CODE -> ' + e.target.id);
// Outputs: NAME -> Afghanistan, CODE -> country
}
render()
{
return (
<select className="form-control country-name" id="country"
onChange={e => this.handleCountryChange(e)} >
{countries().map((country, i) => { return (
<option id={country.code} value={country.name} key={i} >
{country.name}
</option>
)})}
</select>
);
}
使用选择元素上的 onChange 处理程序,我正在尝试检索选项的属性。 country code 和 country name 分别存储在其 id 和 value 属性中。我得到以下结果
输出:名称 -> 阿富汗,代码 -> 国家/地区
这意味着我正在从所选选项中获取选择元素的 id 和值。如何从其上发生的事件中检索活动 <option> 元素的属性,而不是选择元素本身?
【问题讨论】:
标签: reactjs