【问题标题】:React multi select not able to select negative value反应多选无法选择负值
【发布时间】:2019-04-23 23:23:33
【问题描述】:

嗨,我正在使用带有负值和正值的反应多选。当我选择 -1 它会自动更改为 1。因此无法选择 -1。其他值工作正常。

 class FlavorForm extends React.Component {
  constructor(props) {
    super(props);
    this.state = {value: '1'};

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

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

  handleSubmit(event) {
    alert('Your favorite flavor is: ' + this.state.value);
    event.preventDefault();
  }

  render() {
    return (
      <form onSubmit={this.handleSubmit}>
        <label>
          Pick your favorite flavor:
          <select value={this.state.value} onChange={this.handleChange} multiple>
            <option value="-1">Grapefruit</option>
            <option value="0">Lime</option>
            <option value="1">Coconut</option>
            <option value="2">Mango</option>
          </select>
        </label>
        <input type="submit" value="Submit" />
      </form>
    );
  }
}

ReactDOM.render(
  <FlavorForm />,
  document.getElementById('root')
);

请帮助如何使用反应多选选择-1。

Demo

【问题讨论】:

    标签: javascript reactjs multi-select


    【解决方案1】:

    我注意到我的控制台正在记录以下错误

    警告:如果multiple 为真,则提供给的value 属性必须是一个数组。

    我冒昧地修复了这个问题,巧合的是,-1 选择开始起作用了:

    class FlavorForm extends React.Component {
      constructor (props) {
        super(props)
        this.state = { values: [] }
    
        this.handleChange = this.handleChange.bind(this)
        this.handleSubmit = this.handleSubmit.bind(this)
      }
    
      handleChange (event) {
        const values = this.state.values.includes(event.target.value)
          ? this.state.values
          : this.state.values.concat(event.target.value)
        this.setState({ values: values })
      }
    
      handleSubmit (event) {
        alert(`Your favorite flavor is: ${this.state.values}`)
        event.preventDefault()
      }
    
      render () {
        return (
          <form onSubmit={this.handleSubmit}>
            <label>
              Pick your favorite flavor:
              <select
                value={this.state.values}
                onChange={this.handleChange}
                multiple
              >
                <option value="-1">Grapefruit</option>
                <option value="0">Lime</option>
                <option value="1">Coconut</option>
                <option value="2">Mango</option>
              </select>
            </label>
            <input type="submit" value="Submit" />
          </form>
        )
      }
    }
    

    【讨论】:

      【解决方案2】:

      从选择元素中删除 value 属性,它在那里是无效的。

      <select onChange={this.handleChange} multiple>...</select>
      

      【讨论】:

      【解决方案3】:

      您需要将 state 中的 values 属性存储为数组,即:

      this.state = { 
        values: []
      }
      

      然后,将 handleChange 更改为:

      handleChange(event) {
          let choices = event.target.selectedOptions;
          let final = [];
          for(let i=0; i<choices.length; i++) {
            final.push(choices[i].value);
          }
          this.setState({ value: final });
        }
      

      它应该可以工作。

      这是一个工作版本的链接: https://codesandbox.io/s/p3705q0zm

      干杯!

      【讨论】:

        【解决方案4】:

        如果基于documentation 的multiple 为真,则使用数组作为值。 对于多选行为,请使用in this post 所述的选项。

        class FlavorForm extends React.Component {
          constructor(props) {
            super(props);
            this.state = {value: ['1']};
        
            this.handleChange = this.handleChange.bind(this);
            this.handleSubmit = this.handleSubmit.bind(this);
          }
        
          handleChange = ({target}) => {
            this.setState(prevState => ({
              value: [].filter.call(target.options, o => o.selected).map(o => o.value)
            }));
          }
        
          handleSubmit(event) {
            alert('Your favorite flavor is: ' + this.state.value);
            event.preventDefault();
          }
        
          render() {
            return (
              <form onSubmit={this.handleSubmit}>
                <label>
                  Pick your favorite flavor:
                  <select value={this.state.value} onChange={this.handleChange} multiple>
                    <option value="-1">Grapefruit</option>
                    <option value="0">Lime</option>
                    <option value="1">Coconut</option>
                    <option value="2">Mango</option>
                  </select>
                </label>
                <input type="submit" value="Submit" />
              </form>
            );
          }
        }
        
        ReactDOM.render(
          <FlavorForm />,
          document.getElementById('root')
        );
        <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
        <div id="root"></div>

        【讨论】:

        • 但我只需要多选。无论如何感谢您的努力!
        • 我升级了多选行为。
        猜你喜欢
        • 2021-07-21
        • 2021-10-17
        • 1970-01-01
        • 1970-01-01
        • 2021-05-19
        • 2021-08-16
        • 1970-01-01
        • 2021-03-03
        • 2022-09-23
        相关资源
        最近更新 更多