简体   繁体   English

在html select中获取当前选中的onBlur选项

[英]Get currently selected option onBlur in html select

I have a Select element in my react Component: 我的React组件中有一个Select元素:

 <select id={id} value={value || ''} onChange={this.onChange} onMouseOver={this.onMouseOver} onMouseLeave={this.onMouseLeave} onFocus={this.onFocus} onBlur={this.onBlur} > <option value="">Please select a value</option> {items.map(item => ( <option value={item.key} key={item.key}> {item.value} </option> ))} 

On the onBlur function I want to get the value prop of the currently selected option element in the select list. 在onBlur函数上,我想获取选择列表中当前选择的选项元素的值prop。

  onBlur = (e) => { console.log(e.target.value) // want to get the currently selected options value here }; 

You can use state for storing the value of selected data.Then get the data from state when onBlur is called 您可以使用state来存储所选数据的值。然后在调用onBlur时从状态中获取数据

<select
    id={id}
    value={this.state.selectedValue}
    onChange={this.onChange}
    onMouseOver={this.onMouseOver}
    onMouseLeave={this.onMouseLeave}
    onFocus={this.onFocus}
    onBlur={this.onBlur}
>

<option value="">Please select a value</option>
{items.map(item => (
    <option value={item.key} key={item.key}>
        {item.value}
    </option>
))}
onChange(event) {
this.setState({'selectedValue':event.target.value})
}
onBlur() {
console.log(this.state.selectedValue)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM