欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

React之input動態(tài)取值和賦值方式

 更新時間:2023年05月11日 10:48:10   作者:周家大小姐.  
這篇文章主要介紹了React之input動態(tài)取值和賦值方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

react input動態(tài)取值和賦值

需求:對用戶在form表單輸入的值提取出來,并且改變setState中的數(shù)據(jù)

1.在constructor中設(shè)置初始值

2.在Input框中定義

如果只有value沒有onChange事件會報錯,change事件可以關(guān)聯(lián)輸入的值

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

3.對onchange事件注冊,然后獲取Input值再對state賦值

e.target.name

代表你當前輸入Input框?qū)?yīng)的Name,如email,password

e.target.value

代表當前輸入的值

 
    this.setState({
      [e.target.name] : e.target.value
    })
import React, { Component } from 'react'
class Register extends Component {
  // 在構(gòu)造函數(shù)當中設(shè)置狀態(tài)
  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框?qū)?yīng)的Name,如email,password
    // e.target.value 代表當前輸入的值
    this.setState({
      [e.target.name] : e.target.value
    })
  }
 //提交對應(yīng)的內(nèi)容
  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">創(chuàng)建新的賬戶</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框的值

在開發(fā)中,我們比較常見的需要獲取input框的值或者對input框的值判斷是否為空,空的話給出提示

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

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

inputChange里去更新reason的值,reason是input框里的內(nèi)容

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中設(shè)定了初始值

?rebutTip:'none',
?reason:''

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • vue通過點擊事件讀取音頻文件的方法

    vue通過點擊事件讀取音頻文件的方法

    最近做項目遇到這樣的一個需求,通過select元素來選擇音頻文件的名稱,點擊按鈕可以進行試聽。接下來通過本文給大家介紹vue項目中通過點擊事件讀取音頻文件的方法,需要的朋友可以參考下
    2018-05-05
  • Vue 請求傳公共參數(shù)的操作

    Vue 請求傳公共參數(shù)的操作

    這篇文章主要介紹了Vue 請求傳公共參數(shù)的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • 深入淺出分析vue2和vue3的區(qū)別

    深入淺出分析vue2和vue3的區(qū)別

    這篇文章主要介紹了vue2和vue3的區(qū)別,較為詳細的分析了vue2與vue3的常見區(qū)別與使用方法,需要的朋友可以參考下
    2023-06-06
  • Vue中引入swiper報錯的問題及解決

    Vue中引入swiper報錯的問題及解決

    這篇文章主要介紹了Vue中引入swiper報錯的問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • vue2.0在沒有dev-server.js下的本地數(shù)據(jù)配置方法

    vue2.0在沒有dev-server.js下的本地數(shù)據(jù)配置方法

    這篇文章主要介紹了vue2.0在沒有dev-server.js下的本地數(shù)據(jù)配置方法的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2018-02-02
  • SyntaxError:?/xx.vue:?Unexpected?token,?expected?“,“錯誤解決

    SyntaxError:?/xx.vue:?Unexpected?token,?expected?“,“錯誤解

    這篇文章主要為大家介紹了SyntaxError:?/xx.vue:?Unexpected?token,?expected?“,“錯誤解決方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-08-08
  • Vuex的actions屬性的具體使用

    Vuex的actions屬性的具體使用

    這篇文章主要介紹了Vuex的actions屬性的具體使用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • Vue動態(tài)樣式方法實例總結(jié)

    Vue動態(tài)樣式方法實例總結(jié)

    在vue項目中,很多場景要求我們動態(tài)改變元素的樣式,下面這篇文章主要給大家介紹了關(guān)于Vue動態(tài)樣式方法的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2023-02-02
  • 腳手架vue-cli工程webpack的作用和特點

    腳手架vue-cli工程webpack的作用和特點

    webpack是一個模塊打包的工具,它的作用是把互相依賴的模塊處理成靜態(tài)資源。這篇文章主要介紹了vue-cli工程webpack的作用和特點,需要的朋友可以參考下
    2018-09-09
  • Vue獲取初始化數(shù)據(jù)是放在created還是mounted解讀

    Vue獲取初始化數(shù)據(jù)是放在created還是mounted解讀

    這篇文章主要介紹了Vue獲取初始化數(shù)據(jù)是放在created還是mounted的問題解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03

最新評論