如何从React中的输入字段获取表单数据

Leo*_*ban 8 javascript input reactjs

构造函数和函数:

constructor(props) {
    super(props);

    this.state = {
        tagline: 'We rank what people are talking about.',
        year: new Date().getFullYear()
    };

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

onFormSubmit(e) {
    console.log('onFormSubmit', e)
    console.log('this.state', this.state);
};
Run Code Online (Sandbox Code Playgroud)

表单(为清晰起见删除了classNames):

<form onSubmit={ this.onFormSubmit }>
    <div className="fl w100">
        <div>
            <input type="text" id="email" value={ this.state.email }/>
            <label htmlFor="email">Email</label>
        </div>
    </div>

    <div className="fl w100">
        <div>
            <input type="password" id="password" value={ this.state.password }/>
            <label htmlFor="password">Password</label>
        </div>
    </div>

    <button type="submit">
        Login
    </button>
</form>
Run Code Online (Sandbox Code Playgroud)

这是注销,请注意没有电子邮件或密码信息:在此输入图像描述

完整登录组件代码


import React from 'react';

class Login extends React.Component {

    constructor(props) {
        super(props);

        this.state = {
            tagline: 'We rank what people are talking about.',
            year: new Date().getFullYear()
        };

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

    onFormSubmit(e) {
        console.log('onFormSubmit', e)
        console.log('this.state', this.state);
    };

    render() {
        return (<div className="app">
            <div className="welcome">
                <header>
                    <div className="wikitags-logo">
                        <img src="imgs/wikitags-logo.png"/>
                    </div>
                    <h2>Admin Portal</h2>
                    <p>{ this.state.tagline }</p>
                </header>

                <section className="login-form">
                    <form onSubmit={ this.onFormSubmit }>
                        <div className="fl w100">
                            <div className="mdl-textfield mdl-js-textfield">
                                <input className="mdl-textfield__input" type="text" id="email" value={ this.state.email }/>
                                <label className="mdl-textfield__label" htmlFor="email">Email</label>
                            </div>
                        </div>

                        <div className="fl w100">
                            <div className="mdl-textfield mdl-js-textfield">
                                <input className="mdl-textfield__input" type="password" id="password" value={ this.state.password }/>
                                <label className="mdl-textfield__label" htmlFor="password">Password</label>
                            </div>
                        </div>

                        <button type="submit" className="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-button--accent">
                            Login
                        </button>
                    </form>
                </section>

                <footer>
                    &copy; { this.state.year } WikiTags
                </footer>
            </div>
        </div>);
    }
}

export default Login;
Run Code Online (Sandbox Code Playgroud)

May*_*kla 7

意见建议:

1.您正在将value属性与输入字段一起使用,但未定义onChange方法,因此您的输入字段将为只读状态,因为状态值不会被更新。

2.您需要定义一个onChange事件,将所有输入字段都删除,或者通过删除value属性使它们成为不受控制的元素。

3.如果元素不受控制,则定义对每个字段的引用并访问use值this.ref_name.value

通过定义onChange事件:

定义每个输入元素的名称属性(名称应与状态变量名称相同,这将有助于更新状态,我们可以在单个onChange函数中处理所有更改),如下所示:

<input type="text" name='value' value={this.state.value} onChange={(e) => this.handleChange(e)} />

handleChange(e){
   this.setState({[e.target.name]: e.target.value})
}
Run Code Online (Sandbox Code Playgroud)

按Uncotrolled元素:

<input type="text" ref={el => this.el = el} />
Run Code Online (Sandbox Code Playgroud)

现在在onSubmit函数内部,this.el.value用于访问此输入字段的值。

检查此答案以供参考:https : //stackoverflow.com/a/43695213/5185595


bnt*_*zio 6

您没有收到电子邮件或密码信息,因为您正在通过该州console.log('this.state', this.state);并且您尚未设置电子邮件和密码的状态.

现在,您有两个选择:

  1. 设置状态并从那里获取表单信息
  2. 将输入值传递给处理信息的函数

对于选项1,您需要为电子邮件和密码设置状态(尽管不建议设置密码状态)和onChange输入上的事件处理程序.

设置onChange事件处理程序.

<form onSubmit={ this.onFormSubmit }>
  <input type="text" id="email" onChange={this.handleEmailChange} value={ this.state.email } />

  <input type="password" id="password" onChange={this.handlePasswordChange} value={ this.state.password } />

  <button type="submit">
    Login
  </button>
</form>
Run Code Online (Sandbox Code Playgroud)

以及设置emailpassword状态的功能.

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

handlePasswordChange(event) {
  this.setState({ password: event.target.value });
}
Run Code Online (Sandbox Code Playgroud)

并且不要忘记为您emailpassword构造函数初始化状态并绑定函数.

constructor(props) {
  super(props);

  this.state = {
    email: '',
    password: ''
  };

  this.handleEmailChange = this.handleEmailChange.bind(this);
  this.handlePasswordChange = this.handlePasswordChange.bind(this);
}
Run Code Online (Sandbox Code Playgroud)

而且你已经完成了!然后在onFormSubmit函数上只需从状态访问emailpasswordthis.state.email,this.state.password并做任何你喜欢的事情.

现在对于选项2,您可以传入event.target.value输入,这些是电子邮件和密码的值,并将这些值传递给表单事件处理onSubmit函数,从那里你可以做任何你想做的事情(设置状态或更新电子邮件和密码,更改它们,等等).

<form onSubmit={ this.onFormSubmit }>
  <input type="text" id="email" name="theEmail" />

  <input type="password" id="password" name="thePassword" />

  <button type="submit">
    Login
  </button>
</form>
Run Code Online (Sandbox Code Playgroud)

而且onFormSubmit功能.

onFormSubmit(event) {
  const email = event.target.theEmail.value;
  const password = event.target.thePassword.value;

  // do stuff
  console.log('Email:', email);
  console.log('Password:', password);
};
Run Code Online (Sandbox Code Playgroud)

选项2是更容易和推荐的方法来完成你想要做的事情.

请记住,您的应用处理得越少越好.