• react表单提交


    class FlavorForm extends React.Component {
      constructor(props) {
        super(props);
        this.state = {value: 'coconut'};
    
        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 La Croix flavor:
              <select value={this.state.value} 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>
            <label>
              <input value={this.state.value} onChange={this.handleChange}/>
            </label>
            <label>
              <textarea value={this.state.value} onChange={this.handleChange}/>
            </label>
            <input type="submit" value="Submit" />
          </form>
        );
      }
    }
    
    ReactDOM.render(
      <FlavorForm />,
      document.getElementById('root')
    );
    
    class MutilForm extends React.Component {
      constructor(props) {
        super(props);
        this.state = {
          nums: '',
          isGoing: true
        };
        this.handleChange = this.handleChange.bind(this);
      }
      handleChange(e) {
        const target = e.target;
        const value = target.type === 'checkbox'? target.checked: target.value;
        const name = target.name;
        this.setState({
          [name]: value
        });
        console.log(value);
      }
      render() {
        return (
          <form>
          <label>
            <input type='text' name='nums' onChange={this.handleChange} value={this.state.nums}/>  
          </label>
            <br/>
           <label>
            <input type='checkbox' name='isGoing' onChange={this.handleChange} value={this.state.isGoing}/> 
           </label>
          </form>
        );
      }
    }
    let root = document.getElementById('root');
    ReactDOM.render(<MutilForm/>,root);
    
  • 相关阅读:
    android 图片全屏
    .9.png
    C++中的endl
    C++输入输出cin与cout
    word-search
    Java中的的画正三角方法
    octave中的一些基本操作
    C#中判断语句 if、if-else if、switch-case
    C#中的异常处理(try-catch的使用)——使程序更加稳定
    编程&blog处女篇-用C#求100以内的质数
  • 原文地址:https://www.cnblogs.com/dkplus/p/8882462.html
Copyright © 2020-2023  润新知