[英]Serialize form data to json react
我如何在反应中序列化数据?
<form method="post" action="/insert" onSubmit={(e)=>this.addMysql(e)}>
<label>Username</label>
<input type="text" id="username" name="nameUser"/>
<label>Password</label>
<input type="text" id="password" name="passs"/>
<button type="submit" type="submit">Dodaj</button>
</form>
这是我的功能
addMysql(event){
console.log(event.targe) //this outputs an empty form
event.preventDefault();
}
我不想使用状态。 只需 onSubmit 将所有表单数据发送到 AddMysql
https://medium.com/@snirlugassy/generic-input-handler-with-react-js-44a97e22cd0d
谢谢 Sag1v
//So, here's a simple trick i use to get this done: //Using React Hook const [data, setData] = useState({}); //the create an onInputChange function for the Inputs this way const onInputChange = async e =>{ const {name, value} = e.target; //check to validate if entry is not a number if(isNaN(name)){ data[name] = value; //somehow update data setData({...data}) } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.