简体   繁体   English

OnChange 用于存储所选下拉选项的值

[英]OnChange for Storing Value of Selected Drop Down Option

I have a Material UI dropdown menu.我有一个 Material UI 下拉菜单。 I will make a search later on the basis of the selected option from the drop down menu.稍后我将根据从下拉菜单中选择的选项进行搜索。 How exactly could I use onChange() to store the option selected?我究竟如何使用onChange()来存储所选的选项?

For now, I am trying to print the stored value (criteria) using typography at the end but instead of showing the selected value, I get a black page.现在,我尝试在最后使用排版打印存储的值(标准),但没有显示选定的值,而是得到了一个黑页。

export default class userSearchPage extends Component<{}, { searchItem: string, criteria: string }>{

  constructor(props: Readonly<{}>) {
    super(props);
    this.state = {
      searchItem: '',
      criteria: '',
    };
    this.handleDropDownChange = this.handleDropDownChange.bind(this);
  }

  handleDropDownChange(selected: any) {
    this.setState({
        criteria: selected
    });
  }

  render() {
    return (
      <div>
        <PermanentDrawerLeft></PermanentDrawerLeft>
        <div className='main-content'>
          <MuiThemeProvider>
            <DropDownMenu onChange = {this.handleDropDownChange}>
              <MenuItem style={{ fontSize: "20px" }} primaryText="Search By" />
              <MenuItem value={1} style={{ fontSize: "20px" }} primaryText="First Name" />
              <MenuItem value={2} style={{ fontSize: "20px" }} primaryText="Last Name" />
              <MenuItem value={3} style={{ fontSize: "20px" }} primaryText="Phone Number" />
              <MenuItem value={4} style={{ fontSize: "20px" }} primaryText="Email" />
            </DropDownMenu>
          </MuiThemeProvider>
          <Typography>{this.state.criteria}</Typography>
          <br></br><br></br>
          <SearchBar
            onChange={e => this.setState({ searchItem: e })}
            value = {this.state.searchItem}
            onRequestSearch={() => console.log('onRequestSearch')}
            style={{
              margin: '0 auto',
              maxWidth: 800
            }}
          />
           <Typography>{this.state.criteria}</Typography>
        </div>
      </div>
    );
  }
}

How could I fix this?我怎么能解决这个问题?

Note: This is typescript注意:这是打字稿

Added添加

export default class userSearchPage extends Component<{}, { searchItem: string, criteria: any}>{

  constructor(props: Readonly<{}>) {
    super(props);
    this.state = {
      searchItem: '',
      criteria: null, 
    };
    this.handleDropDownChange = this.handleDropDownChange.bind(this);
  }


  handleDropDownChange(event: any) {
    console.log(event.target.value);
    this.setState({
      criteria: event.target.value
    });
  }

  render() {
    return (
      <div>
        <PermanentDrawerLeft></PermanentDrawerLeft>
        <div className='main-content'>
        <InputLabel id="demo-simple-select-label">Search By</InputLabel>
          <Select
            value={this.state.criteria}
            onChange={this.handleDropDownChange}
            displayEmpty
          >
            <MenuItem disabled value="  ">
              <em>Search By</em>
            </MenuItem>
            <MenuItem value={1}>First Name</MenuItem>
            <MenuItem value={2}>Last Name</MenuItem>
            <MenuItem value={3}>Phone Number</MenuItem>
            <MenuItem value={4}>Email</MenuItem>
          </Select>
        </div>
      </div>
    );
  }
}

Assuming the DropDownMenu component is just a Material-UI Select component ( https://material-ui.com/components/selects/#select ), you need to update the state of your searchItem to the value of the selected MenuItem.假设 DropDownMenu 组件只是一个 Material-UI Select 组件( https://material-ui.com/components/selects/#select ),您需要将 searchItem 的状态更新为所选 MenuItem 的值。

<DropDownMenu onChange={event => {this.setState({searchItem: event.target.value})}>

Note, currently in your example the first name and last name item are using the same value of 1.请注意,目前在您的示例中,名字和姓氏项使用相同的值 1。

Update - added the comment that contained the solution's code:更新- 添加了包含解决方案代码的注释:

Here's an example using Material-UI's Select component instead of DropDownMenu: https://codesandbox.io/s/eager-hugle-tlfri这是使用 Material-UI 的 Select 组件而不是 DropDownMenu 的示例: https : //codesandbox.io/s/eager-hugle-tlfri

DropDownMenu 菜单应具有接收选定选项作为参数的 'onChange' 事件,您可以将其存储在组件状态中

You could use the onChange event to do something when the user is selecting an option.您可以使用 onChange 事件在用户选择选项时执行某些操作。 If you want information, you could look at this article: https://www.w3schools.com/jsref/event_onchange.asp如果你想要信息,你可以看看这篇文章: https : //www.w3schools.com/jsref/event_onchange.asp

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

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