简体   繁体   English

如何使 ReactJS onClick function 更改文本区域的值?

[英]How to make ReactJS onClick function change the value of a textarea?

I am trying to make a ReactJS form in which there is a button which, once pressed, alters the value of an input tag.我正在尝试制作一个 ReactJS 表单,其中有一个按钮,一旦按下,就会改变输入标签的值。 Every time I press the button, the text is changed, but the page reloads, and the textarea returns to being blank.每次我按下按钮时,文本都会更改,但页面会重新加载,并且 textarea 会恢复为空白。 I've searched up on this issue a little and found this thread: Reactjs every time refreshing page on setState .我对这个问题进行了一些搜索,发现了这个线程: Reactjs 每次在 setState 上刷新页面 However, the solution to this problem( shouldComponentUpdate() {return false;} ) ended up making it so that the text of the inputarea didn't change at all.然而,这个问题的解决方案( shouldComponentUpdate() {return false;} )最终使得输入区域的文本根本没有改变。 Here is my code:这是我的代码:

 import React, { Component } from 'react'; export default class Header extends Component { state = { cep: "", address: "", } searchCEP(){ this.setState({ address: "Adress" }); } render(){ return ( <div> <div id="host-form" className="row"> <div className="col s1"></div> <form className="col s10"> <div className="row"> <div className="input-field col s6"> <input placeholder='"Terreno para churrasco"' id="title" type="text" className="validate"/> <label htmlFor="title">Título</label> </div> <div className="input-field col s2"> <input id="cep" type="text" className="validate" defaultValue={this.state.cep}/> <label htmlFor="cep">CEP</label> </div> <div id="buscar-cep" className="col s1"> <button onClick={() => this.searchCEP()} className="waves-effect blue waves-light btn">Buscar por CEP</button> </div> <div className="col s2"></div> </div> <div className="row"> <div className="input-field col s12"> <input placeholder='"Terreno de 500 metros quadrados com uma linda vista do Cristo Redentor...\"' id="description" type="text" className="validate"/> <label htmlFor="description">Descrição</label> </div> </div> <div className="row"> <div className="input-field col s12"> <input id="address" type="text" className="validate" defaultValue={this.state.address}/> <label htmlFor="address">Endereço</label> </div> </div> </form> </div> </div> ); } };
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

I hope this is enough to understand my problem.我希望这足以理解我的问题。 Thanks in advance for any help.提前感谢您的帮助。

You can make following changes您可以进行以下更改

 <div id="buscar-cep" className="col s1">
                                <button onClick={(e) => this.searchCEP(e)} className="waves-effect blue waves-light btn">Buscar por CEP</button>
 </div>

and in searchCEP function, do the following changes:并在searchCEP function 中,进行以下更改:

searchCEP(e){
e.preventDefault();
        this.setState({ address : "Adress" });
    }

This will stop the reloading这将停止重新加载

from reading what you said the best case reason as to why the page reloads, from past exprience is that when you call the function you do not prevent the default action of the button within the form so you could try从阅读您所说的关于页面重新加载的最佳案例原因来看,从过去的经验来看,当您调用 function 时,您不会阻止表单中按钮的默认操作,因此您可以尝试

    """
  searchCEP(e){
        e.preventDefault();
        this.setState({ address : "Adress" });
    }
"""

 <div id="buscar-cep" className="col s1">
  <button onClick={(e) => this.searchCEP(e)} className="waves-effect blue waves-light btn">Buscar por CEP</button>
  </div>

"""

Since自从

hope this helps希望这可以帮助

You should stop the default behavior of the form element:您应该停止form元素的默认行为:

<form onSubmit={(e) => e.preventDefault()}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM