React之input动态取值和赋值方式

 更新时间:2023年05月11日 10:48:10   作者:周家大小姐.  
这篇文章主要介绍了React之input动态取值和赋值方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

react input动态取值和赋值

需求:对用户在form表单输入的值提取出来,并且改变setState中的数据

1.在constructor中设置初始值

2.在Input框中定义

如果只有value没有onChange事件会报错,change事件可以关联输入的值

value = {this.state.name}
onChange ={this.onChange.bind(this) }

3.对onchange事件注册,然后获取Input值再对state赋值

e.target.name

代表你当前输入Input框对应的Name,如email,password

e.target.value

代表当前输入的值

 
    this.setState({
      [e.target.name] : e.target.value
    })
import React, { Component } from 'react'
class Register extends Component {
  // 在构造函数当中设置状态
  constructor(props){
    super(props)
    this.state ={
      name : '',
      email:'',
      password:'',
      password2:'',
      errors:{},//用户不合法信息提示
    }
  }
  onChange(e){
    // console.log(e.currentTarget.value)
    console.log(e.target.name)//(e.target.name代表你当前输入Input框对应的Name,如email,password
    // e.target.value 代表当前输入的值
    this.setState({
      [e.target.name] : e.target.value
    })
  }
 //提交对应的内容
  onSubmit(e){
    e.preventDefault()
    const newUser = {
      name : this.state.name,
      email:this.state.email,
      password:this.state.password,
      password2:this.state.password2,
    }
    console.log(newUser)
  } 
  render() {
    return (
      <div className="register">
        {/* {user ? user.name : null} */}
        <div className="container">
          <div className="row">
            <div className="col-md-8 m-auto">
              <h1 className="display-4 text-center">注册</h1>
              <p className="lead text-center">创建新的账户</p>
              <form onSubmit={this.onSubmit.bind(this)}>
                <div className="form-group">
                  <input
                    type="text"
                    className="form-control form-control-lg"
                    placeholder="用户名"
                    name="name"
                    value = {this.state.name}
                    onChange ={this.onChange.bind(this) }
                  />
                </div>
                <div className="form-group">
                  <input
                    type="email"
                    className='form-control form-control-lg'
                    placeholder="邮箱地址"
                    name="email"
                    value = {this.state.email}
                    onChange ={this.onChange.bind(this) }
                    />
                  <small className="form-text text-muted">我们使用了gravatar全球公认头像, 如果需要有头像显示, 请使用在gravatar注册的邮箱</small>
                </div>
                <div className="form-group">
                  <input
                    type="password"
                    className='form-control form-control-lg'
                    placeholder="密码"
                    name="password"
                    value = {this.state.password}
                    onChange ={this.onChange.bind(this) }
                  />
                </div>
                <div className="form-group">
                  <input type="password"  
                    className='form-control form-control-lg'  
                    placeholder="确认密码"  
                    name="password2" 
                    value = {this.state.password2}
                    onChange ={this.onChange.bind(this) }
                     />
                </div>
                <input type="submit" className="btn btn-info btn-block mt-4" />
              </form>
            </div>
          </div>
        </div>
      </div >
    )
  }
}
export default Register;

react获取input框的值

在开发中,我们比较常见的需要获取input框的值或者对input框的值判断是否为空,空的话给出提示

首先在input框添加一个onchange事件

<TextArea type="text"  rows={4} value={reason} onChange={inputChange}/>

inputChange里去更新reason的值,reason是input框里的内容

function inputChange(e){
        dispatch({
            type:'buyBackManage/updateState',
            payload:{
                reason:e.target.value
            },
        });
    }

给按钮一个点击事件

<Button type="primary" size={'large'} onClick={()=>rebut(reason)}>驳回</Button>

rebut是去更改某个变量的值,我这里是修改rebutTip的值,由原来的none变成block

function rebut(reason){
        console.log(reason)
        if(reason.length===0)
        {
            dispatch({
                type:'buyBackManage/updateState',
                payload:{
                    rebutTip:'block'
                },
            });
        }
        console.log('123')
    }

上面的reason和rebutTip一开始在models中设定了初始值

 rebutTip:'none',
 reason:''

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Vue3动态组件component不生效问题解决方法

    Vue3动态组件component不生效问题解决方法

    动态组件component是Vue中非常实用的一个功能,它可以根据条件动态切换不同的组件,在Vue3中使用方法和Vue2基本一致,在vue3使用component动态组件展示组件时,组件就是不展示显示空白,所以本文记录了Vue3动态组件component不生效问题解决方法,需要的朋友可以参考下
    2024-08-08
  • vue监视器@Watch创建执行immediate方式

    vue监视器@Watch创建执行immediate方式

    这篇文章主要介绍了vue监视器@Watch创建执行immediate方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-08-08
  • vue2项目之swiper.js 的使用

    vue2项目之swiper.js 的使用

    这篇文章主要介绍了vue2项目之swiper.js 的使用,需要的朋友可以参考下
    2023-12-12
  • vue项目打包后打开页面空白解决办法

    vue项目打包后打开页面空白解决办法

    这篇文章主要介绍了vue项目打包后打开空白解决办法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-06-06
  • vue、react等单页面项目应该这样子部署到服务器

    vue、react等单页面项目应该这样子部署到服务器

    这篇文章主要介绍了vue、react等单页面项目应该这样子部署到服务器,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-01-01
  • vue父子组件的数据传递示例

    vue父子组件的数据传递示例

    这篇文章主要介绍了vue父子组件的数据传递示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-03-03
  • vue将后台数据时间戳转换成日期格式

    vue将后台数据时间戳转换成日期格式

    这篇文章主要为大家详细介绍了vue将后台数据时间戳转换成日期格式,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-07-07
  • VUE3刷新页面报错问题解决:Uncaught SyntaxError:Unexpected token '<'

    VUE3刷新页面报错问题解决:Uncaught SyntaxError:Unexpected token &apo

    这篇文章主要介绍了VUE3刷新页面报错:Uncaught SyntaxError: Unexpected token ‘<‘,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-03-03
  • vue从后台渲染文章列表以及根据id跳转文章详情详解

    vue从后台渲染文章列表以及根据id跳转文章详情详解

    这篇文章主要给大家介绍了关于vue从后台渲染文章列表以及根据id跳转文章详情的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-12-12
  • 详解如何在vue项目中使用layui框架及采坑

    详解如何在vue项目中使用layui框架及采坑

    这篇文章主要介绍了vue使用layui框架,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-05-05

最新评论