如何获取超链接将其名称作为POST变量传递?

时间:2011-04-07 09:51:59

标签: php javascript

我正在尝试创建一个包含超链接的表单。单击时,它们会提交页面并传递不同的值。

这是我的代码:

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

当我点击href链接时,我收到错误

document.MyForm.submit is not a function
[Break On This Error] document.MyForm.submit(); 

有人知道为什么会出现这个错误吗?

3 个答案:

答案 0 :(得分:1)

使用id =“MyForm”而不是name =“MyForm”...

答案 1 :(得分:0)

尝试:

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

答案 2 :(得分:0)

我遇到了同样的问题。这是因为name='submit'中的<input type=hidden name='submit' value='" + action +"'>。你改变这个来使它工作:

试试这个:

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