2011-09-02 51 views
0

我简化了我的代码,但在最终的web应用中,在页面上有〜100个表单而不是这两个表单。我的问题是什么是使我的链接提交表单与JavaScript的最佳方式。我现在所做的并不是很明显,因为有多个名为supporttype的字段。什么是最好的方式来做我想要做的大规模的〜100表格。使用多个链接提交的Javascript表单

<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> 

回答

0

您可以构建动态的形式:

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(); 
} 
+0

如果你发现自己写黑客做这样的post请求,这意味着服务器部分必须有一些设计问题。 – timdream

+0

这实际上正是我所需要的,谢谢。 – Andrew

0

最简单的办法 - 把值Form 1和Form到相应的输入值:

<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> 

然后写通用JS提交最接近点击链接的表单:

function getsupport (link) { 
    var form = link.parentNode; 
    while (form.tagName != "FORM") { 
     form = form.parentNode; 
    } 
    form.submit(); 
}