Javascript form submitting with multiple links
I simplified my code for this question but in my final webapp there is ~100 forms on a page instead of the two here. My question is what is 开发者_C百科the best way to make my links submit forms with javascript. What I have now doesn't work obviously because there are multiple fields called supporttype. Whats the best way to do what I want to do for a large scale of ~100 forms.
<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>
You can construct the form dynamically:
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();
}
The easiest way - put the values form1 and form2 into values of the corresponding inputs:
<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>
And then write generic JS for submitting the form that is nearest to the clicked link:
function getsupport ( link ) {
var form = link.parentNode;
while (form.tagName != "FORM") {
form = form.parentNode;
}
form.submit();
}
精彩评论