How I can get the hyperlinks to pass their names as POST variables?
I'm trying to create a form with hyperlinks. When clicked, they submit the page and pass different values.
Here's my code:
<script language="Javascript">
function submitForm(action){
document.forms["MyForm"].innerHTML =
document.forms["MyForm"].innerHTML
+ "<input type=hidden name=submit value='" + 开发者_StackOverflowaction +"'>";
document.forms["MyForm"].submit();
}
</script>
<form name="MyForm" action="test.php" method="post">
<input type="text" name="abc" value="" />
<a name="edit" href="javascript:void(1);" onClick="submitForm('Edit');">Edit</a>
<a name="delete" href="javascript:void(1);" onClick="submitForm('Delete');">Delete</a></form>
When I click on href link, I get the error
document.MyForm.submit is not a function
[Break On This Error] document.MyForm.submit();
Anyone know why this error occurs?
Use id="MyForm" instead of name="MyForm"...
I had the same problem. It's because of name='submit'
in <input type=hidden name='submit' value='" + action +"'>
. You change this to make it work:
Try this:
<script>
function submitForm(action)
{
document.forms["MyForm"].innerHTML =
document.forms["MyForm"].innerHTML
+ "<input type=hidden name='submit1' value='" + action +"'>";
document.forms["MyForm"].submit();
}
</script>
<form name="MyForm" id="MyForm" action="config.php" method="post">
<input type="text" name="abc" value="" />
<a name="edit" href="javascript:void(1);" onClick="submitForm('Edit');">Edit</a>
<a name="delete" href="javascript:void(1);" onClick="submitForm('Delete');">Delete</a>
</form>
try:
<script>
function setAndSubmit(text){
document.getElementById('submit').value=text;
document.forms["MyForm"].submit()
}
</script>
<form name="MyForm" id="MyForm" action="test.php" method="post">
<input type="text" name="abc" value="" />
<input type="hidden" name="submit" id="submit" value="" />
<a name="edit" href="#" onClick="setAndSubmit('Edit')">Edit</a>
</form>
精彩评论