我如何在 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
最佳答案
我建议你使用 states
因为那是 Reacty 的方式。如果没有,我可能会提出一些建议。
通常我会建议使用 FormData
API,但 IE 和旧版浏览器不支持它。
我认为this answer应该让你开始,因为它是 serialize
的普通 JS 版本。
最后serialize
如果您正在使用 jQuery,我认为您没有或不应该使用它。
关于javascript - 将表单数据序列化为json react,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47592396/