使用多个链接提交Javascript表单
我简化了这个问题的代码,但在我的最后一个webapp中,一个页面上有大约100个表单,而不是这里的两个。我的问题是,使用javascript让我的链接提交表单的最佳方式是什么。我现在使用的显然不起作用,因为有多个名为supporttype的字段。什么是最好的方式来做什么,我想做的大规模约100形式使用多个链接提交Javascript表单,javascript,forms,post,hyperlink,Javascript,Forms,Post,Hyperlink,我简化了这个问题的代码,但在我的最后一个webapp中,一个页面上有大约100个表单,而不是这里的两个。我的问题是,使用javascript让我的链接提交表单的最佳方式是什么。我现在使用的显然不起作用,因为有多个名为supporttype的字段。什么是最好的方式来做什么,我想做的大规模约100形式 <html> <head> <script language="JavaScript" type="text/javascript"> <!-- functi
<html>
<head>
<script language="JavaScript" type="text/javascript">
<!--
function getsupport ( selectedtype )
{
document.albumdl.supporttype.value = selectedtype ;
document.albumdl.submit() ;
}
-->
</script>
</head>
<body>
<form name="albumdl" method="post" action="processLinks.php">
<input type="hidden" name="supporttype" />
<a href="javascript:getsupport('form1')">Form1 </a>
</form>
<form name="albumdl" method="post" action="processLinks.php">
<input type="hidden" name="supporttype" />
<a href="javascript:getsupport('form2')">From2</a>
</form>
</body>
</html>
您可以动态构造表单:
function getSupport (type) {
var f = document.createElement('form'),
input = document.createElement('input');
f.setAttribute('action', 'processLinks.php');
f.setAttribute('method', 'post');
input.setAttribute('name', 'supporttype');
input.value = type;
f.appendChild(input);
f.submit();
}
最简单的方法-将form1和form2的值放入相应输入的值中:
<form name="albumdl" method="post" action="processLinks.php">
<input type="hidden" name="supporttype" value="form1" />
<a href="javascript:submitForm(this)">Form1 </a>
</form>
<form name="albumdl" method="post" action="processLinks.php">
<input type="hidden" name="supporttype" value="form2" />
<a href="javascript:submitForm(this)">From2</a>
</form>
如果您发现自己正在编写这样的hack-to-do-post请求,这意味着服务器部分一定存在一些设计问题。
function getsupport ( link ) {
var form = link.parentNode;
while (form.tagName != "FORM") {
form = form.parentNode;
}
form.submit();
}