第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

reactJS <select>不更新狀態(tài)

reactJS <select>不更新狀態(tài)

回首憶惘然 2021-04-02 10:07:43
我有一個reactJS應用程序,在這里我提示用戶進行一些輸入。這是要求用戶從<select>對象中選擇一個選項并在輸入框中輸入文本字符串的代碼:<div className="container">    <div className="row">        <div className="col-12 test-left text_15">            <label>Select one of the following questions to answer</label>        </div>    </div>    <div className="row">        <div className="col-12 text_15">            <select className="text_15"> value={currentComponent.state.securityQuestion} onChange={(event) => this.saveQuestion(event)}>                <option value="0">What is you mother's maiden name?</option>                <option value="1">What elementary school did you attend?</option>                <option value="2">What was the name of your first pet?</option>                <option value="3">What city were you born in?</option>            </select>        </div>    </div></div><div className="spacerAfterButton"></div> <div className="container">    <div className="row">        <div className="col-12 text-left text_15">            <label>Provide the answer to the question you selected</label>        </div>    </div>    <div className="row">        <div className="col-12 text-center">            <input type="text" maxLength="20" className={localData.cssAnswer} onChange={(event) => this.saveAnswer(event)}/>             </div>    </div></div>我的狀態(tài)如下所示:    this.state = {         passData: this.props.location.state,        planID: "",        memberID: "",        PIN: "",        userID: "",        password: "",        securityQuestion: "",        securityAnswer: "",        eMail: ""     }我也有以下內容:saveQuestion(event) {        let currentComponent = this;      var localQuestion = event.target.value;    console.log("localQuestion: ", localQuestion);    currentComponent.setState({securityQuestion: localQuestion}); }當我執(zhí)行該應用程序并滾動或單擊選擇選項時,我的控制臺永遠都不會顯示任何內容。在此過程的結尾,我將使用所選值和輸入的文本字符串構建一個字符串。為了進行測試,我在輸入框中輸入“測試字符串”,然后選擇了第一個選擇選項。我本來希望在生成的字符串中看到值“ 0test字符串”,但是卻得到“測試字符串”,證明狀態(tài)變量沒有通過附加到select的onChange進行更新。有什么建議?
查看完整描述

3 回答

?
慕桂英4014372

TA貢獻1871條經驗 獲得超13個贊

您選擇的代碼中有錯字:


<select className="text_15"> value={currentComponent.state.securityQuestion} onChange={(event) => this.saveQuestion(event)}>

    <option value="0">What is you mother's maiden name?</option>

    <option value="1">What elementary school did you attend?</option>

    <option value="2">What was the name of your first pet?</option>

    <option value="3">What city were you born in?</option>

</select>

應該:


<select className="text_15" value={currentComponent.state.securityQuestion} onChange={(event) => this.saveQuestion(event)}>

    <option value="0">What is you mother's maiden name?</option>

    <option value="1">What elementary school did you attend?</option>

    <option value="2">What was the name of your first pet?</option>

    <option value="3">What city were you born in?</option>

</select>

您的標簽上還有一個額外的>select。


查看完整回答
反對 回復 2021-04-08
?
慕桂英546537

TA貢獻1848條經驗 獲得超10個贊

您設置狀態(tài)的方式將丟失您以前的狀態(tài)信息。

currentComponent.setState({ ...this.state,securityAnswer:localAnswer });

...this.state就是所謂的價差,它將保留您不想更改的狀態(tài)元素。


查看完整回答
反對 回復 2021-04-08
?
白衣染霜花

TA貢獻1796條經驗 獲得超10個贊

將函數(shù)綁定到this,或使用箭頭函數(shù)語法:


saveQuestion = (event) => {    

  let currentComponent = this;  

  var localQuestion = event.target.value;

  console.log("localQuestion: ", localQuestion);

  this.setState({securityQuestion: localQuestion});

}


查看完整回答
反對 回復 2021-04-08
  • 3 回答
  • 0 關注
  • 403 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號