简体   繁体   English

如何在不刷新页面的情况下将表单的数据提交到另一个函数?

[英]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的参数,但是当我尝试不改变任何内容时。

Here's my form: 这是我的表格:

<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>

and here's my function: 这是我的功能:

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.

相关问题 提交表单数据,而不用$ ajax刷新页面 - Submit form data without refreshing the page with $ajax 提交表单数据而无需刷新整个页面 - Submit form data without refreshing entire page 如何在不刷新页面的情况下自动提交两个forms? - How do I automatically submit two forms without the page refreshing? 提交后如何重设电子邮件表单,以便可以发送另一封电子邮件而无需刷新页面 - How do I reset an email form after submission so that another email can be sent without refreshing the page 如何在不将页面刷新到mysql数据库的同时刷新页面的情况下创建表单? - How do I create a form without refreshing the page at the same time the data goes into a mysql data base? 如何通过单击复选框而不刷新页面来发布表单? - How do I post form by clicking a checkbox without refreshing the page? AJAX / JQuery:提交表单和调用功能而无需刷新页面 - AJAX/JQuery: Submit form and call function without refreshing page Codeigniter:使用 jQuery ajax 提交表单数据而不刷新页面 - Codeigniter: submit form data without page refreshing with jQuery ajax 如何使用 ajax 在不刷新页面的情况下提交表单? - How to submit a form without page refreshing using ajax? 提交表单而不使用ajaxForm刷新页面 - Submit a form without refreshing a page using ajaxForm
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM