[英]How do I submit a form's data to another function without refreshing the page?
I'm working on a collection of web apps using REACT JS. 我正在使用REACT JS开发一组Web应用程序。 For part of the current app I'm working on, I have a modal that renders on a state change and has a form to receive a name with some related data.
对于我正在开发的当前应用程序的一部分,我有一个模态,用于呈现状态更改,并具有一种形式来接收带有一些相关数据的名称。 I want the submit button in this form to submit the code to a
submitNewName()
function which will compare the submitted name & data to names & data from a JSON file. 我希望此表单中的提交按钮将代码提交给
submitNewName()
函数,该函数将比较提交的名称和数据与JSON文件中的名称和数据。 Unfortunately, I cannot test to see if any of my code works because the page refreshes upon submission, which refreshes the developer console. 不幸的是,由于页面在提交后刷新,因此刷新了开发者控制台,因此我无法测试我的代码是否有效。
Within my submitNewName()
function, I have the following line of code: var newName = document.getElementById("newNameForm").submit()
. 在我的
submitNewName()
函数中,我具有以下代码行: var newName = document.getElementById("newNameForm").submit()
。 I read another similar question where someone suggested adding function(e) {e.preventDefault();}
as an argument for .submit
, but when I tried that it didn't change anything. 我读了另一个类似的问题,有人建议添加
function(e) {e.preventDefault();}
作为.submit
的参数,但是当我尝试不改变任何内容时。
<form id="addNameForm" className="RNGpopupTXT">
Name: <input type="text" name="name"/>
<br/><br/>
Type: <select>
<option value="fname">First Name</option>
<option value="lname">Last Name</option>
<option value="sname">Single Name (e.g. Madonna)</option>
<option value="title">Title</option>
</select>
<br/><br/>
Gender: <select>
<option value="mg">Male</option>
<option value="fg">Female</option>
<option value="ng">Non-specific</option>
</select>
<br/><br/>
Tags: <input type="text" size="40" name="tags" placeholder=" eg. 'Star Wars,Scifi,Space'"/>
<br/><br/><br/><br/>
<div align="center">
<input type="submit" value="Submit" className="mdc-button" style={{ textDecoration: 'none' }} onClick={() => this.submitNewName()}/>
</div>
</form>
submitNewName() {
var newName = document.getElementById("newNameForm").submit(function(e) {
e.preventDefault();
});
}
I would like the data from the form to be given to the function in a way that would allow it to be compared to the JSON file data. 我希望将表单中的数据以一种可以与JSON文件数据进行比较的方式提供给函数。 I also do not want the page to refresh, as that refreshes the state of the page, closing the modal prematurely.
我也不希望页面刷新,因为这会刷新页面状态,过早关闭模式。 When I run the program now, it does send an error message to the console, but I cannot read what it says because the console is refreshed with the web page.
现在运行该程序时,它确实向控制台发送了一条错误消息,但是由于控制台随网页一起刷新,因此我无法阅读其中的内容。
It feels like you could use more React to make your life easier here. 感觉您可以在此处使用更多的React来使您的生活更轻松。
You don't have to use the form's onSubmit
event. 您不必使用表单的
onSubmit
事件。 You could just add an onClick
handler to the button. 您可以只向按钮添加
onClick
处理程序。 In that function, you could do all the comparing you want and, whrn you're ready, it can do the submitting logic too. 在该函数中,您可以进行所需的所有比较,并且在准备就绪时也可以进行提交逻辑。
If you wanted to compare the form's values, you might want to keep those values in state. 如果要比较表单的值,则可能需要保持这些值的状态。 To do so though, you would need
onChange
functions on each of the form elements to update the state as the user provides input. 但是,要这样做,您将需要在每个表单元素上使用
onChange
函数以在用户提供输入时更新状态。
As I didn't see much React code in your example, I took the liberty of writing some out. 因为在您的示例中没有看到太多的React代码,所以我自由地编写了一些代码。 Hopefully it will help you:
希望它将对您有所帮助:
https://codesandbox.io/s/elastic-shape-yrv0b https://codesandbox.io/s/elastic-shape-yrv0b
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.