javascript JQuery提交隐藏表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4783608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 14:52:41  来源:igfitidea点击:

JQuery Submit Hidden Form

javascriptjqueryforms

提问by CogitoErgoSum

I know this is simple but I can't figure it out today and I can't find the right solution.

我知道这很简单,但我今天无法弄清楚,也找不到正确的解决方案。

I have a series of tabs which populate data. At the end is a confirmation tab. At this point they can click a 'submit' button. When this button is pressed a hidden form is populated with all the relevant data. I'd like this form to then be submitted but I can't figure out how to submit the form with JQUery. I've attempted to do a trigger on the button but perhaps I'm doing it wrong. Below is a sample of the code for the button taht gets clicked. Note the values populate, I just can't get the form to submit

我有一系列填充数据的选项卡。最后是一个确认选项卡。此时,他们可以单击“提交”按钮。当按下此按钮时,一个隐藏的表单将填充所有相关数据。我希望然后提交此表单,但我不知道如何使用 JQUEry 提交表单。我试图在按钮上触发,但也许我做错了。下面是单击按钮的代码示例。请注意填充的值,我只是无法提交表单

  $('#submitAppointment').click(function() {
        $("#" + formName + "schedule_id option[value='" + $("input[name='appointmentTime']:checked").val() + "']").attr('selected', 'selected');      
        $("#" + formName + "apt_date").val($("#confirmDate").text());
        $("#" + formName + "first_name").val($("#confirmFirstName").text());
        $("#" + formName + "last_name").val($("#confirmLastName").text());
        $("#" + formName + "email").val($("#confirmEmail").text());
        $("#" + formName + "phone").val($("#confirmPhone").text());
        $("#" + formName + "notes").val($("#confirmNotes").text());
        $('#appt_form').submit(function() {
            alert('Handler for .submit() called.');
            return false;
        });
    });

Any ideas on what I'd need to do to rigger the submit function?

关于我需要做什么来操纵提交功能的任何想法?

回答by Pointy

How about

怎么样

$('#appt_form').submit();

Don't return falsefrom the handler function if you want the form submission to actually proceed. If your intention is to have the form post without the page refreshing, then you're talking about an ajax operation for which you could use $.post(). (It's not clear that that's what you want to do however.)

false如果您希望表单提交实际继续,请不要从处理程序函数返回。如果您的意图是在不刷新页面的情况下发布表单,那么您正在谈论可以使用的 ajax 操作$.post()。(目前尚不清楚这是否是您想要做的。)

回答by Day

Assuming the form you want to submit is found by the #appt_formselector, just use .submitin the no-argument form (which is simply an alias for .trigger('submit'))

假设#appt_form选择器找到了您要提交的表单,只需在无参数表单中使用.submit(这只是 的别名.trigger('submit')

$('#appt_form').submit();

回答by Marcin Pieciukiewicz

I think you should try calling

我想你应该试着打电话

$('#appt_form').submit()

$('#appt_form').submit()

if appt_form is your form id. That command should submit the form, based on jQuery documentation.

如果 appt_form 是您的表单 ID。该命令应根据 jQuery 文档提交表单。

Here is jQuery submit function documentation

这是jQuery提交功能文档