Home > Web Front-end > JS Tutorial > Introduction to the usage of React form elements (with code)

Introduction to the usage of React form elements (with code)

不言
Release: 2019-04-04 16:41:46
forward
2180 people have browsed it

This article brings you an introduction to the usage of React form elements (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. help.

Today we will talk about react form elements.

Controlled elements

Let’s take a look at how to get the value of the input box

import React, { Component } from 'react';

class Trem extends React.Component {
    constructor(props){
        super(props);
        this.inp = this.inp.bind(this);
        this.sub = this.sub.bind(this);
        this.state = {
            place:"请输入...",
            inputV:''
        }
    };
    inp(e){
        this.setState({
            inputV:e.target.value     {/* 通过事件细节改变inputV的值*/}
        });
        console.log(e.target.value)
    };    
    sub(){
      console.log(this.state.inputV)
    };    
    render(){
        return (
            <div>
                <input type="text" onChange={this.inp} placeholder={this.state.place} value={this.state.inputV}/>
                <br/>
                <button onClick={this.sub}>{this.props.main}</button>{/*此处的main是来自父组件的传值*/}
            </div>
        )
    }
}
export default Trem;
Copy after login

Code interpretation:
this.inp = this. inp.bind(this); Bind the inp function this to point to
this.state Initialize the variable
inp() Monitor the input value of the input
this.state.inputV Obtain the value of the input through assignment

textarea tag

import React, { Component } from 'react';

class Trem extends React.Component {
    constructor(props){
        super(props);
        this.inp = this.inp.bind(this);
        this.sub = this.sub.bind(this);
        this.state = {
            place:"请输入...",
            inputV:''
        }
    };
    inp(e){
        this.setState({
            inputV:e.target.value    
        });
        console.log(e.target.value)
    };    
    sub(){
      console.log(this.state.inputV)
    };    
    render(){
        return (
            <div>
                <textarea type="text" onChange={this.inp} placeholder={this.state.place} value={this.state.inputV}/>                
                <br/>
                <button onClick={this.sub}>{this.props.main}</button>
            </div>
        )
    }
}

export default Trem;
Copy after login

Drop-down selection box

import React, { Component } from 'react';

class Trem extends React.Component {
    constructor(props){
        super(props);
        this.select = this.select.bind(this);
        this.state = {
            selectV:'coconut'
        }
    };    
    select(e){
        this.setState({
            selectV:e.target.value    
        });
        console.log(e.target.value)
    };        
    render(){
        return (
            <div>                
                <select value={this.state.selectV} onChange={this.select}>
                    <option value="grapefruit">Grapefruit</option>
                    <option value="lime">Lime</option>
                    <option value="coconut">Coconut</option>
                    <option value="mango">Mango</option>
                </select>
                <br/>
            </div>
        )
    }
}

export default Trem;
Copy after login

Code interpretation:
Please note that the Coconut option is initially selected due to the selected attribute Chosen. In React, the previous selected attribute is not used, but the value attribute is used on the root select tag to represent the selected item. This is more convenient in controlled components, since you only need to update the component in one place.

In short, ,