2011-09-02 48 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(); 
}