[英]Send single hidden field with form using javascript
Currently I'm working with ASP.Net
and a nested form. 目前,我正在使用
ASP.Net
和嵌套表单。 My problem is, that ASP.Net
only allows one form per page, but I need a kind of sub-form to redirect to another page. 我的问题是,
ASP.Net
每页只允许一个表单,但是我需要一种子表单来重定向到另一页。
My idea was to create a submit
using JavaScript, but I don't get how to set the content of the post
, which will be send. 我的想法是使用JavaScript创建一个
submit
,但我不知道如何设置将发送的post
的内容。
For example I've tried to use the following code: 例如,我尝试使用以下代码:
<div>
<input type="hidden" value="ABCDE" />
<input title="Send Form" onclick="this.form.method = 'post'; this.form.action = 'https://externpage/url'; this.form.submit();" type="submit" />
</div>
My problem is, that the content of the request should only be the hidden fields
containing ABCDE
and not all fields on the page. 我的问题是,请求的内容应仅是包含
ABCDE
的hidden fields
,而不是页面上的所有字段。 How can i achieve this using JavaScript
and HTML
? 如何使用
JavaScript
和HTML
实现此目的?
Thanks a lot! 非常感谢!
Use FormData() to do the encoding. 使用FormData()进行编码。
sendForm=(e)=>{
let data = new FormData();
let hidden = document.querySelector('button').parentNode.firstChild.value;
data.append('hidden',hidden);
let xhr = new XMLHttpRequest();
xhr.onreadystatechange=()=>(xhr.readyState == 4) ? console.log(xhr.response) : null;
xhr.open('POST','http://yoururl.com');
xhr.send(data);
}
document.addEventListener('DOMContentLoaded',()=>{
document.querySelector('button').addEventListener('click',sendForm);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.