【问题标题】:Simple way to get value from DropDown menu in ReactJS从 ReactJS 的下拉菜单中获取价值的简单方法
【发布时间】:2019-12-02 11:27:09
【问题描述】:

我试图确保存储用户从 ReactJS 的下拉菜单中选择的正确值。我在这里修改现有代码并显示我认为有助于理解问题的部分。谁能告诉我正确设置参数值的简单方法?直到现在我无法存储价值

非常感谢

emphasized textimport React from "react";

import { Centered } from "meteor/empirica:core";

export default class ExitSurvey extends React.Component {
  static stepName = "ExitSurvey";
  state = { age: "", gender: "", country: "", strength: "", fair: "", feedback: "" , education: "", occupation: "xxxx"};


  handleChange = event => {
    const el = event.currentTarget;
    this.setState({ [el.name]: el.value });
  };

  handleSubmit = event => {
    event.preventDefault();
    this.props.onSubmit(this.state);
  };

  selectCountry (val) {
    this.setState({ country: val });
  }



  render() {
    const { player } = this.props;
    const { age, gender, country, strength, fair, feedback, education, occupation} = this.state;

 return (
      <Centered>
        <div className="exit-survey">
          <h1> Exit Survey </h1>
          <p>
            Please submit the following code to receive your bonus:{" "}
            <strong>{player._id}</strong>.
          </p>
          <p>
            You final <strong>bonus</strong> is in addition of the{" "}
            <strong>1 base reward</strong> for completing the HIT.
          </p>
          <br />
          <p>
            Please answer the following short survey. You do not have to provide
            any information you feel uncomfortable with.
          </p>
          <form onSubmit={this.handleSubmit}>

   <div>
          <label><b>Occupation:</b></label>
            <div>

              <select value={this.state.value}  onChange={this.handleChange}> 

                <option value="Personal-care">Personal care and service occupations</option>
                <option value="Sales">Sales and related occupations</option>
                <option value="Office-Admin">Office and administrative support occupations</option>
                <option value="Farming-Fishing">Farming, fishing, and forestry occupations</option>
                <option value="Construction">Construction and extraction occupations</option>
                <option value="Installation-Maintenance">Installation, maintenance, and repair occupations</option>
                <option value="Production">Production occupations</option>
                <option value="Transportation">Transportation and material moving occupations</option>
                <option value="Military">Military specific occupations</option>
            </select>
            </div>
          </div>

              <p> hello :::: {this.state.occupation}</p>

【问题讨论】:

    标签: html reactjs select drop-down-menu


    【解决方案1】:

    您正在访问state.value,即undefined,因为您所在的州没有名为value 的属性。为了解决这个问题,您需要为您的状态中的 select 元素添加一个属性。

    state = {
      selectedValue: ''
      // this could also be a value for one of its options
      // e.g selectedValue: 'Personal-care'
    
      // add rest of your state values...
    };
    

    您的handleChange 函数基本上是存储元素的名称并为其分配值。但是您还没有在状态中初始化该属性。因此,当组件第一次呈现时,您将没有为 select 元素设置任何值。

    要解决此问题,请修改您的 handleChange 函数以反映以下内容...

    handleChange = event => {
      const { value } = event.currentTarget;
      this.setState({ selectedValue: value });
    };
    

    然后,在您的渲染函数中,更新 select 元素以访问存储在状态中的属性。

    <select value={this.state.selectedValue}  onChange={this.handleChange}>
    

    【讨论】:

    • 以上内容不是您的代码中的内容。除非你的意思是你已经用上面的代码更新了你的代码。
    • 这与我的代码中的这条语句类似 --> handleChange = event => { const el = event.currentTarget; this.setState({ [el.name]: el.value }); };
    • 我已经更新了我的答案,以解释您的情况以及您在哪里遇到了一些问题
    • 我已经尝试过了,我试图通过在屏幕上打印值来确保它正在工作并且它没有更新 -->

      hello :::: {this.state .selectedValue}

    • 我还想问一下是否可以有多个 handleChange() 函数,因为我无法为调查页面中的其他参数更改它。
    猜你喜欢
    • 2019-08-21
    • 1970-01-01
    • 2015-05-20
    • 1970-01-01
    • 2019-05-23
    • 1970-01-01
    • 2021-09-30
    • 1970-01-01
    相关资源
    最近更新 更多