React.js <select>不触发onChange

时间:2018-06-26 11:00:05

标签: javascript html reactjs select

我使用                 {选项}                      );     };     渲染=()=> {         让列表= this.getList();         返回(             
{list}
        );     }; } 导出默认的TestList; 为什么没有在每次单击项目时触发我的onChange?

2 个答案:

答案 0 :(得分:0)

更改

中的代码
 <select size={20} onChange={this.handleSelectListChange}>

<select size={20} onChange={this.handleSelectListChange.bind(this)}>

答案 1 :(得分:0)

它正常工作。

import { Component } from "react";

import React from "react";

class TestList extends Component {

handleSelectListChange = event => {
    console.log("CLICKED " + event.target.value);
};

getList = () => {
    let options = [];
    for (let i = 0; i < 10; i++) {
        options.push(<option key={i} value={i}>{"ITEM " + i}</option>);
    }

    return (
        <select size={20} onChange={this.handleSelectListChange}>
            {options}
        </select>
    );
};

render = () => {
    let list = this.getList();

    return (
        <div>{list}</div>
    );
};
}
export default TestList;

enter image description here