javascript 预填充并提交隐藏表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16741910/
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-27 05:49:03  来源:igfitidea点击:

Prepopulate and submit hidden form

javascriptjqueryhtml

提问by fumeng

I have a form and here's the handler for the submit button:

我有一个表单,这里是提交按钮的处理程序:

$( '#submit_btn' ).click( function( data ){ 
    theForm = document.getElementById( 'realForm' );
    theForm.meetingName.value = document.getElementById( 'meeting_name' ).value;
    theForm.meetingId.value = '';
    theForm.description.value = document.getElementById( 'mtg_description' ).value;
    theForm.startTime.value = startDate + ' ' + startTime;
    theForm.endTime.value = endDate + ' ' + endTime;
    theForm.loginName.value = participants;
    theForm.role.value = roles;
    theForm.docRights.value = docRights;
    theForm.submit();
});

This handler basically pre-processes some data and sends to a hidden form, this:

这个处理程序基本上预处理了一些数据并发送到一个隐藏的表单,这个:

<form id="realForm" style="visibility:hidden" action="/app/meeting/create" method="post">
    <input name="loginName" type="text">
    <input name="meetingName" type="text">
    <input name="meetingId" type="text">
    <input name="startTime" type="text">
    <input name="endTime" type="text">
    <input name="description" type="text">
    <input name="roles" type="text">
    <input name="docRights" type="text">
</form>

Problem is that the request isn't hitting the endpoint defined in the hidden form. What am I doing wrong here?

问题是请求没有到达隐藏表单中定义的端点。我在这里做错了什么?

I've changed to make the input types hidden instead of the form. The submit handler certainly executes and, using FireBug, I don't see the request going out under the NET tab.

我已更改为隐藏输入类型而不是表单。提交处理程序肯定会执行,并且使用 FireBug,我看不到 NET 选项卡下发出的请求。

I'm using this dummy data to try and trigger the request but it's still not working:

我正在使用这个虚拟数据来尝试触发请求,但它仍然无法正常工作:

theForm.meetingName.value       = "MY MTG";
                        theForm.meetingId.value         = '';
                        theForm.description.value       = "DESC";
                        theForm.startTime.value         = "2013-05-25 00:00:00";
                        theForm.endTime.value           = "2013-05-25 02:00:00";
                        theForm.loginName.value         = "[email protected]";
                        theForm.role.value              = "M,M";
                        theForm.docRights.value         = "CRUT,CRUT";

采纳答案by fumeng

The problem is that my first form had a button with type = "submit"...so that was submitting the form even though I didn't want it to.

问题是我的第一个表单有一个 type = "submit" 的按钮......所以即使我不想要它也正在提交表单。

I had to change the type to "button" in order to prevent that from happening.

我不得不将类型更改为“按钮”以防止这种情况发生。

Thanks for all the prompt responses.

感谢所有及时的回复。

回答by Faron

2 tips:

2 小贴士:

  1. use this for fetching variable from input.

    $("#NAME_OF_YOUR_INPUT_ID").val(); 
    
  2. use hidden input instead and identify each one with ID.

    <input id="docRights" type="hidden">
    
  1. 使用它从输入中获取变量。

    $("#NAME_OF_YOUR_INPUT_ID").val(); 
    
  2. 改用隐藏输入并用 ID 标识每个输入。

    <input id="docRights" type="hidden">
    

回答by Adam Plocher

Your inputname is rolesnot role.

你的input名字roles不是role

Change your line of JS to:

将您的 JS 行更改为:

theForm.roles.value = roles;

See JS Fiddle: http://jsfiddle.net/jav6s/

见JS小提琴:http: //jsfiddle.net/jav6s/

回答by Mark H

I'd try this out:

我会试试这个:

document.forms.realForm.submit()