【问题标题】:link onclick does not submit form [duplicate]链接onclick不提交表单[重复]
【发布时间】:2020-09-14 01:37:27
【问题描述】:

我有一个 db 记录回显并格式化在一个表中,每个表行上都有一个删除链接(图标),当从 db 中获取数据时,使用 while 循环进行索引。 单击删除图标时,我希望特定的索引行从 db 和 table 中删除。 这是我的代码:

<script>
function submitForm(){
document.getElementById("myform").submit()
}</script>
</head>
<body>
<table>
<tbody>

 <?php 

$rowIndex = 0;
while($row = mysqli_fetch_row($result){
 $user = $row[0];
 $address=$row[1];
 $phone = $row[2];
echo '<tr>
<td>'.$user.'</td>
<td>'.$address.'</td>
<td>'.$phone.'</td>
<td><a href="" onclick="javascript: submitForm()">Delete(icon)</a>
</tr>'
$rowIndex+=1;

}
?>
</tbody>
</table>
<form method ="post" id ="myform" action ="test.php" >
<input type="hidden" name="del">
</form>

单击“删除”链接时,我希望 javascript 提交表单并移至“test.php”(虽然我打算保持在同一个表单上,但这样我就会知道我的表单实际上已提交)。请任何帮助将不胜感激。

【问题讨论】:

  • 您的浏览器控制台中是否出现任何错误?你试过onclick="submitForm()"吗?您是否尝试过使用&lt;button&gt; 而不是&lt;a&gt;
  • 试试onclick="submitForm(); return false"
  • Tnx 菲尔!请你解释为什么返回错误。仅此一项就使代码工作!请稍微解释一下,我会投票作为这个 qxn 的答案
  • return false 阻止链接导航到其href 属性值

标签: javascript php


【解决方案1】:
<!DOCTYPE html>
<html>
<head>
<title>test javascript form test </title>
</head>
<body>
<form action="#" method="post">
<label>Username</label>
<input type="text" id="name"><br>
<label>Password</label>
<input type="password" id="password"><br>
<button onclick="data()" type="button">Submit</button>
</form>
<script src="https://code.jquery.com/jquery-3.5.1.js"></script>
<script>
function data(){
   var name = $("#name").val();
   var pass = $("#password").val();
   alert(name);
   alert(pass);
}
</script>
</body>
</html>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-14
    • 1970-01-01
    • 1970-01-01
    • 2014-06-11
    • 2015-08-19
    • 2014-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多