javascript 通过javascript提交表单并传递一个变量

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

submitting form through javascript and passing a variable with it

javascripthtml

提问by Jeffrey Klinkhamer

I want to submit a form and add a javascript variable with it. I tried using AJAX but that didn't work properly because the form is cut in two parts on the same page. I'm now using a <button onclick='submit_form()'>send</button>which calls on the following function:

我想提交一个表单并用它添加一个 javascript 变量。我尝试使用 AJAX,但无法正常工作,因为表单在同一页面上被分为两部分。我现在使用 a <button onclick='submit_form()'>send</button>which 调用以下函数:

function submit_form()
{
document.getElementById("form2").submit();
}

however I need to pass on a javascript variable current_scoreI have it declared and it has a value I just don't know how to add this to my submit function I tried using a return within the function itself and writing a function for it but neither worked :) help and hints are greatly appreciated. The idea is that people receive a score fill a form and send it to a database, The ajax script part was to try and pass the values on to the next page that will submit the data

但是我需要传递一个 javascript 变量current_score我已经声明它并且它有一个值我只是不知道如何将它添加到我的提交函数我尝试在函数本身中使用返回并为其编写函数但都没有工作:) 非常感谢帮助和提示。 这个想法是人们收到一个分数填写表格并将其发送到数据库,ajax 脚本部分是尝试将值传递到将提交数据的下一个页面

回答by karim79

Your question is not very clear. The simple way would be to append a get parameter to the URL you are requesting. The following example will append a hidden input element to your form:

你的问题不是很清楚。简单的方法是将 get 参数附加到您请求的 URL。以下示例将向您的表单附加一个隐藏的输入元素:

var hidden = document.createElement("input");
hidden.type = "hidden";
hidden.name = "theName";
hidden.value = current_score;
var f = document.getElementById("form2");
f.appendChild(hidden);
f.submit();

回答by Rene Wooller

You could store the information in window.variable also

您也可以将信息存储在 window.variable 中