简体   繁体   English

使用JavaScript发送带有表单的单个隐藏字段

[英]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. 我的问题是,请求的内容应仅是包含ABCDEhidden fields ,而不是页面上的所有字段。 How can i achieve this using JavaScript and HTML ? 如何使用JavaScriptHTML实现此目的?

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.

相关问题 尝试使用从Javascript到C#的隐藏字段发送值 - Trying to send value using Hidden Field from Javascript to C# 使用Jquery / Javascript将imgsrc值发送到隐藏字段 - Using Jquery/Javascript to send imgsrc value to hidden field 使用Javascript将参数从URL传递到隐藏的表单字段(Gridview) - Using Javascript to pass a parameter from a URL to a hidden form field (Gridview) 无法使用JavaScript或jQuery设置隐藏的表单字段 - Cannot set a hidden form field using JavaScript nor with jQuery 使用Javascript将自定义查询字符串变量提取到隐藏的表单字段中 - Pull Custom Query String Variable into Hidden Form Field Using Javascript 使用表单隐藏字段将JavaScript数组发布到RoR控制器 - Post javascript array to RoR controller using a form hidden field 将javascript函数数据传递到隐藏的表单字段中,并将结果作为url变量发送 - Pass javascript function data into hidden form field and send result as url variable 通过javascript填充html表单中的隐藏字段 - Filling a hidden field in a html form through javascript 检测是否使用隐藏表单字段启用了javascript - Detecting if javascript is enabled with hidden form field 使用Javascript设置隐藏字段值; 使用PHP post方法发送数据 - Set hidden field value using Javascript; use PHP post method to send the data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM