document.getElementById()。submit()不提交

时间:2014-05-15 05:05:31

标签: javascript php html forms

正如标题中所述......出于某种原因,我无法通过点击提交表单但是对于我在同一页面上提交的其他表单似乎正常。我错过了什么?

这是我在该部分的代码

<form id = ".$i." name='edit_staff'  class='form-inline' enctype='multipart/form-data' action = '/DTS/index.php/Index/editInfoStaff' method = 'POST'>";
    echo "<input type = 'hidden' name = 'surname' value = ".$surname." />";
    echo "<input type = 'hidden' name = 'firstName' value = ".$firstName." />";
    echo "<input type = 'hidden' name = 'middleInitial' value = ".$middleInitial." />";
    echo "<input type = 'hidden' name = 'division' value = ".$division." />";
    echo "<input type = 'hidden' name = 'email' value = ".$email." />";
    echo "<input type = 'hidden' name = 'id' value = ".$id." />";
    echo "<a href = '#' onclick='document.getElementById(".$i.").submit()'/>Edit</a>";
    echo "</form>";

    echo "<form id = 'delStaff".$i."' name='delete_staff'  class='form-inline' enctype='multipart/form-data' action = '/DTS/index.php/delete_info/deleteInfoStaff' method = 'POST'>";
    echo "<input type = 'hidden' name = 'surname' value = ".$surname." />";
    echo "<input type = 'hidden' name = 'firstName' value = ".$firstName." />";
    echo "<input type = 'hidden' name = 'middleInitial' value = ".$middleInitial." />";
    echo "<input type = 'hidden' name = 'division' value = ".$division." />";
    echo "<input type = 'hidden' name = 'email' value = ".$email." />";
    echo "<input type = 'hidden' name = 'id' value = ".$id." />";
    echo "<a href = '#' onclick='document.getElementById(delStaff".$i.").submit()'/>Delete</a>";
    echo "</div>";
    echo "</div>";
    echo "</form>";
    $i++;

2 个答案:

答案 0 :(得分:2)

更新如下: 第一种形式:

echo "<a href = '#' onclick='document.getElementById(\"".$i."\").submit()'/>Edit</a>";

第二种形式:

echo "<a href = '#' onclick='document.getElementById(\"delStaff".$i."\").submit()'/>Delete</a>";

试试吧。希望这会有所帮助..

答案 1 :(得分:1)

您在

中的ID旁边缺少引号
document.getElementById(delStaff".$i.").submit()

但根本问题是你滥用echo会导致很难处理引号。假设你想在这里插入引号,你必须逃避它们:

echo "<a href = '#' onclick='document.getElementById(\"delStaff".$i."\").submit()'/>Delete</a>";

而不是

echo "<a href = '#' onclick='document.getElementById(delStaff".$i.").submit()'/>Delete</a>";
echo "</div>";
你应该

<a href='#' onclick="document.getElementById('delStaff<?php echo $i;?>').submit()"/>Delete</a>
</div>

当然,正确分离javascript和html会更好。