代码之家  ›  专栏  ›  技术社区  ›  Alex

在表单提交后设置selectbox默认值

  •  0
  • Alex  · 技术社区  · 7 年前

    如何使用react js将选择框选择重置为表单提交后的第一个选择?

    请参阅以下代码片段:

    import React, { Component } from "react";
    import { RouteComponentProps } from "react-router";
    
    export class Contactform extends Component {
      constructor(props) {
        super(props);
    
        this.state = {
            name: '',
            email: '',
            payment: [],
            loading: true
        };
    
        this.handleChange = this.handleChange.bind(this);
        this.handleSubmit = this.handleSubmit.bind(this);
      }
    
      componentDidMount(){    
        fetch('http://www.json-generator.com/api/json/get/cgBGpTYqKW?indent=2')
        .then((res) => res.json())
        .then((data) => {
          this.setState({ name: data[0].name, email:data[0].email, payment:data[0].payment, loading: false });
        })
      }
    
      handleChange(event) {
        this.setState({
            [event.target.name]: event.target.value
        })
      }
    
      handleSubmit(event) {
        event.preventDefault();
        const form = {
         name: this.state.name,
         email: this.state.email,
         payment: this.state.selectbox
        }
    
        var database = [];
        database.push(form);
        this.setState({
             name: '',
             email: ''
             //How to set a selectbox default value?
        })
      }
    
        render() {
    
          if(this.state.loading) {
            return <span>Loading...</span>
          } else {
            return (
              <div>
                  <form onSubmit={this.handleSubmit}>
    
                            <select className="form-control" name="selectbox" onChange={this.handleChange}>
                              {this.state.payment.map((payment) => 
                                <option key={payment.value} value={payment.value}>{payment.text}</option>
                              )}
                            </select>
    
                        <button type="submit">Submit</button>
                  </form>
              </div>
            );
          }
        }
      }
    

    谢谢

    2 回复  |  直到 7 年前
        1
  •  1
  •   Prabhu    7 年前

    更改代码如下

    import React, { Component } from "react";
    import { RouteComponentProps } from "react-router";
    
    export class Contactform extends Component {
      constructor(props) {
        super(props);
    
        this.state = {
            name: '',
            email: '',
            payment: [],
            loading: true,
            selectbox:"", //set your default value here 
        };
    
        this.handleChange = this.handleChange.bind(this);
        this.handleSubmit = this.handleSubmit.bind(this);
      }
    
      componentDidMount(){    
        fetch('http://www.json-generator.com/api/json/get/cgBGpTYqKW?indent=2')
        .then((res) => res.json())
        .then((data) => {
          this.setState({ name: data[0].name, email:data[0].email, payment:data[0].payment, loading: false });
        })
      }
    
      handleChange(event) {
        this.setState({
            [event.target.name]: event.target.value
        })
      }
    
      handleSubmit(event) {
        event.preventDefault();
        const form = {
         name: this.state.name,
         email: this.state.email,
         payment: this.state.selectbox
        }
    
        var database = [];
        database.push(form);
        this.setState({
             name: '',
             email: ''
             selectbox: "" // Change the default value here
        })
      }
    
        render() {
    
          if(this.state.loading) {
            return <span>Loading...</span>
          } else {
            return (
              <div>
                  <form onSubmit={this.handleSubmit}>
    
                            <select className="form-control" name="selectbox" onChange={this.handleChange} value={this.state.selectbox}>
                              {this.state.payment.map((payment) => 
                                <option key={payment.value} value={payment.value}>{payment.text}</option>
                              )}
                            </select>
    
                        <button type="submit">Submit</button>
                  </form>
              </div>
            );
          }
        }
      }
    
        2
  •  0
  •   dungmidside    7 年前

    你可以储存一个 defaultValue 在你的班级里 selectValue 状态控制selectbox的值

    this.defaultValue = null;
    
    this.state = {
        selectValue: defaultValue;
    }
    
    <form onSubmit={this.handleSubmit}>
        <select 
            className="form-control" 
            name="selectbox" 
            onChange={this.handleChange}
            value={this.state.selectValue}
        >
            {this.state.payment.map((payment) => 
            <option key={payment.value} value={payment.value}>{payment.text}</option>
            )}
        />
        <button type="submit">Submit</button>
    </form>
    
    handleSubmit() {
        // submit ...
        this.setState({
            this.state.selectValue: defaultValue
        })  
    }
    

    不完全正确,但主要思想是这样的。对不起我的英语不好:)