如何在React中的<select>中获取所选<option>的索引?

时间:2019-10-20 00:24:54

标签: reactjs select

我想从<option>获取所选<select>的索引。我尝试使用jQuery,直接获取对象的属性(这表明_this2.selector.current未定义)和selectedIndex,但似乎都不起作用。

<div className="cell-content">
    <select
        id="common-name-selector"
        className="form-control"
        onChange={() => {
            this.handleChange();
            presentSpecies = this.selector.target.value;
        }}
        ref={this.selector}
    >
        {(this.selector.current = 0)}
        <option value="" selected>
            Select by Common Name
        </option>
        {
            data.data.species.map((option, index) => {
                return (
                    <option
                        value={option.index}
                        key={index}
                    >
                        {option.commonName}
                    </option>
                );
        })}
    </select>

    {
        //This is the part I'm having trouble with.
        setInterval(() => {
            console.log(this.selector.current);
            // presentSpecies = 0;
            presentSpecies = this.selector.current.querySelector;
            ReactDOM.render(
                <Router />
                document.getElementById("root")
            );
        }, 500);
    }
</div>

0 个答案:

没有答案