如何使用此 javascript 函数将变量发送到表单?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4855430/
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
How can I send a variable to a form using this javascript function?
提问by NCoder
I've got this onclick call:
我有这个 onclick 电话:
onClick="mySubmit();
which calls this function:
它调用这个函数:
function mySubmit(){
document.getElementById("myForm").submit();
}
which then submits this form:
然后提交此表单:
<form id="myForm" action="action.php" method="post">
My question is:how do I send a variable to the form from the onClick to get something like <form id="myForm" action="action.php?id=**(the variable sent from the onclick goes here)**" method="post">
我的问题是:如何从 onClick 向表单发送变量以获取类似<form id="myForm" action="action.php?id=**(the variable sent from the onclick goes here)**" method="post">
Thanks!
谢谢!
回答by Juan Mendes
Easiest way: append a hidden field to the form.
最简单的方法:在表单中附加一个隐藏字段。
<form id="myForm" action="action.php" method="post">
<input type='hidden' id= 'hiddenField' name='id' value='' />
<script>
function mySubmit() {
document.getElementById('hiddenField').value = "Whatever I want here";
document.getElementById("myForm").submit();
}
</script>
Or use a function like
或者使用像这样的函数
function addHiddenField(form, props) {
Object.keys(props).forEach(fieldName => {
var field = form[fieldName];
if (!field) {
field = document.createElement('input');
field.type = 'hidden';
field.name = fieldName;
form.appendChild(field);
}
field.value = props[fieldName];
});
}
document.querySelector('form').addEventListener('submit', () => {
addHiddenField(this, {
someQueryName: 'someQueryValue',
otherQueryName: 'otherVal'
});
});
<form>
Name
<input name=name />
<input type=submit />
</form>
Note that you can use DevTools to modify the iframe's sandbox to allow it to submit forms and you can verify the posted URL. sandbox="... allow-forms"
请注意,您可以使用 DevTools 修改 iframe 的沙箱以允许它提交表单,并且您可以验证发布的 URL。 sandbox="... allow-forms"
回答by Waqas Raja
place a input type hidden inside the form then submit the form
将输入类型隐藏在表单中,然后提交表单
<input id="id" name="id" type="hidden" />
set the value of the hidden field in your javascript submit()
在 javascript submit() 中设置隐藏字段的值
document.getElementById('id').value = **;
but by setting form method="post" the id will not be the part of query string, i.e. the url will remain action.php instead if you really want the id in query string i.e. url action.php?id=** then you need to change the form method="get", by this the hidden field id will automatically be the part of the url i.e action.php?id=**
但是通过设置 form method="post" id 将不是查询字符串的一部分,即 url 将保留 action.php 而不是如果你真的想要查询字符串中的 id ie url action.php?id=** 那么你需要更改表单method="get",这样隐藏字段id将自动成为url的一部分,即action.php?id=**
read about difference between get and post
here is how you access posted value on next pageif you really need to use method="post" action="action.php"
如果您确实需要使用 method="post" action="action.php",这是访问下一页上发布的值的方法
回答by Waqas Raja
Your HTML :
你的 HTML :
<form id="myForm" action="#" method="post">
<input type='hidden' id="id" name='id' value='123' />
<input type='submit' name='submit' value='Click me !' onclick='addParam()' />
</form>
Your Script :
你的脚本:
function addParam() {
var url = "action.php?id=" + document.getElementById('id').value;
document.getElementById("myForm").setAttribute('action', url);
}
Thank You.
谢谢你。