简体   繁体   中英

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='" + action +"'>";

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?

使用id =“ MyForm”代替name =“ MyForm” ...

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>

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM