简体   繁体   English

如何动态生成HTML表单,包括动作,输入和JavaScript提交?

[英]How to dynamically generate HTML form including action and input and submit in JavaScript?

I am generating a form using JavaScript and when user clicks and submits the form, I'd like it go invoke my function which will dynamically update this form with new input and new values as well as update the action value of the form and I want my function to automatically submit this updated form. 我正在使用JavaScript生成表单,当用户单击并提交表单时,我希望它调用我的函数,该函数将使用新的输入和新值动态更新此表单,并更新该表单的操作值,我想要我的功能是自动提交此更新的表单。

I am doing something like below currently, but not sure if this is the right approach as it doesn't seem to work: 我目前正在做下面的事情,但不确定这是否正确,因为它似乎不起作用:

document.writeln('<form action="" id="myForm" onsubmit="return processMyForm(this);">');

And in my processMyForm(form) function, I am doing something like below: 在我的processMyForm(form)函数中,我正在执行以下操作:

form.setAttribute('method',"POST");
form.setAttribute('action', "http://new.target.host/path");
var node = document.createElement("INPUT");
node.setAttribute("type", "hidden");
node.setAttribute("name", "fieldName");
node.setAttribute("value","fieldValue");
form.appendChild(node.cloneNode());
form.submit();

Any idea how I can dynamically generate a form and submit it to some other site so that it will behave just like clicking a submit button on a form that is already has everything exactly the way that I want? 知道如何动态生成表单并将其提交到其他站点,以便其行为就像单击已经具有所需功能的表单上的“提交”按钮一样吗?

You can substitute using FormData and XMLHttpRequest() for appending html <form> to document . 您可以使用FormDataXMLHttpRequest()代替将html <form>附加到document

Create an array including objects having properties "name" , "value" , at click on <input type="button"> within existing <form> prevent default form submission, iterate both array and existing form elements, appending the values to FormData object. 创建一个包含对象的数组,这些对象具有属性"name""value" ,在现有的<form>中单击<input type="button">可以防止默认表单提交,迭代数组和现有form元素,并将值附加到FormData对象。

Use XMLHttpRequest() to POST FormData object to server at .send() . 使用XMLHttpRequest()POST FormData在目标服务器.send()

 var props = [{name:"fieldName1", value:"fieldValue1"}]; var url = "http://new.target.host/path"; var form = document.querySelector("form"); form.submit.addEventListener("click", function(event) { event.preventDefault(); var fd = new FormData(); var [...inputs] = [...form.querySelectorAll("input:not([name=submit])") , ...props]; for (let input of inputs) { console.log(input.name, input.value); fd.append(input.name, input.value); }; // var request = new XMLHttpRequest(); // request.open("POST", url); // request.send(fd); }) 
 <form> <input type="text" name="fieldName0" value="fieldName0"/> <input type="button" name="submit" value="submit"/> </form> 

I lost a link where following it worked, but basically the key was to create a form object and be able to call a submit() function on it. 我失去了遵循它的地方的链接,但基本上,关键是创建一个表单对象并能够在其上调用Submit()函数。 I've tried it before and not sure why I kept getting a message about submit() wasn't a method on the form object that I had, but that's exactly what I needed and below is the example: 我之前曾尝试过,但不确定为什么我一直收到关于commit()的消息不是我拥有的表单对象上的方法,但这正是我所需要的,下面是示例:

var $formFinal=$(document.createElement('form')).css({display:'none'}).attr("method","POST").attr("action","https://your.target.host/url");
var $input=$(document.createElement('input')).attr('name','key1').val('value1');
var $input2=$(document.createElement('input')).attr('name','key2').val('value2');
$formFinal.append($input).append($input2);
$("body").append($formFinal);
$formFinal.submit();                

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM