javascript 更改 react.js 中的选择值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47549506/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 07:27:52  来源:igfitidea点击:

Change select value in react.js

javascriptreactjs

提问by Thomas Pecriaux

I need your help !

我需要你的帮助 !

I'm on a project for my compagny and I should create a select field that can be duplicate with React. So, I have a little problem when I want to save my selection, if I refresh the page, the default option still the same (and not the selected one). There is my code for select.js:

我正在为我的公司做一个项目,我应该创建一个可以与 React 重复的选择字段。所以,当我想保存我的选择时,我遇到了一个小问题,如果我刷新页面,默认选项仍然相同(而不是选定的选项)。有我的 select.js 代码:

import React, { Component, PropTypes } from 'react';

class Select extends React.Component {

  constructor(props) {
    super(props);
    this.state = {value: ''};

    this.handleChange = this.handleChange.bind(this);
  }

  handleChange(data) {
    this.setState({value:data.value});
  }

  render() {
    return (
        <label>
          <select className="widefat" name={this.props.name} onChange={this.handleChange}>
            <option value="grapefruit">Grapefruit</option>
            <option value="lime">Lime</option>
            <option value="coconut">Coconut</option>
            <option value="mango">Mango</option>
          </select>
        </label>
    );
  }
}

export default Select;

I change the default value :

我更改了默认值:

When i change the select option

当我更改选择选项时

After a refresh

刷新后

I think it's because in select.js It initialize the value to '' and don't save the selection but I don't know how to save the selection.

我认为这是因为在 select.js 中它将值初始化为 '' 并且不保存选择但我不知道如何保存选择。

回答by 3Dos

Here's a way to accomplish this:

这是实现此目的的方法:

import React, { Component, PropTypes } from 'react';

class Select extends Component {

  constructor(props) {
    super(props);
    this.state = { value: props.value }; // can be initialized by <Select value='someValue' />
  }

  handleChange(event) {
    this.setState({value: event.target.value});
  }

  render() {
    return (
        <label>
          <select className="widefat" value={this.state.value} name={this.props.name} onChange={this.handleChange.bind(this)}>
            <option value="grapefruit">Grapefruit</option>
            <option value="lime">Lime</option>
            <option value="coconut">Coconut</option>
            <option value="mango">Mango</option>
          </select>
        </label>
    );
  }
}

export default Select;

Going further

走得更远

You could iterate in a map in the render method to implement this like so:

您可以在渲染方法中的地图中迭代以实现如下:

  render() {
    const dictionary = [
      { value: 'grapefruit', label: 'Grapefruit' },
      { value: 'lime', label: 'Lime' },
      { value: 'coconut', label: 'Coconut' },
      { value: 'mango', label: 'Mango' }
    ];

    return (
        <label>
          <select
            className="widefat"
            value={this.state.value}
            name={this.props.name}
            onChange={this.handleChange}
          >
            {dictionary.map(
              // Iterating over every entry of the dictionary and converting each
              // one of them into an `option` JSX element
              ({ value, label }) => <option key={value} value={value}>{label}</option>
            )}
          </select>
        </label>
    );
  }

回答by Pablo

The target event property returns the element that triggered the event. It stores a lot of properties, print it to the console, that would familiarize with its capabilities

目标事件属性返回触发事件的元素。它存储了很多属性,将其打印到控制台,以便熟悉其功能

import React, { Component } from 'react';

class Select extends Component {

  constructor(props) {
    super(props);
    this.state = { value: '' };
  }

  handleChange = e => this.setState({ value: e.target.value });

  render() {
    return (
     <label>
       <select className="widefat" name={this.props.name} onChange={this.handleChange}>
        <option value="grapefruit">Grapefruit</option>
        <option value="lime">Lime</option>
        <option value="coconut">Coconut</option>
        <option value="mango">Mango</option>
      </select>
    </label>
    );
  }
}

export default Select;

回答by Thomas Pecriaux

After a long journey to search in documentation and in the depth of internet I found my answer. I forgot to add a "for" for my label. There is my final code :

经过漫长的文档搜索和互联网深度搜索,我找到了答案。我忘了为我的标签添加一个“for”。有我的最终代码:

    import React, { Component, PropTypes } from 'react';

class Select extends React.Component {

  constructor(props) {
    super(props);
    this.handleChange = this.handleChange.bind(this);
  }

  handleChange(event) {
    this.setState({value: this.props.value});
  }

  render() {
    return (
        <label htmlFor={this.props.id}>{this.props.label}
          <select defaultValue={this.props.value}  id={this.props.id} className="widefat" name={this.props.name} onChange={this.handleChange.bind(this)}>
            <option>Aucun</option>
            <option value="55">Option 2</option>
            <option value="126">Backend configuration & installation</option>
            <option value="125">Frontend integration</option>
            <option value="124">Graphic Design</option>
          </select>
        </label>
    );
  }
}

export default Select;