php - 一种表单中的常规按钮和提交按钮不起作用

标签 php javascript forms

我有一个从 PHP 请求返回的表单。该表单有一个提交按钮和一个常规按钮。提交似乎工作得很好,但常规的不行!请指教。

这是我的 HTML:

<tr id='actionRow' name='actionRow' hidden='hidden'>
    <td>
        <input class='actionBtn' type='submit' id='confirm' name='confirm' 
            value='Confirm' onclick='genChanged();'/> 
    </td>
    <td>                                
        <input class='actionBtn' type='button' id='cancel' name='cancel' 
            value='Cancel' onclick='cancel();' /> 
    </td>
</tr> 

这是 cancel() 函数:

function cancel(){
        alert('in cancel');
        document.getElementById('editRow').hidden = false;
        document.getElementById('actionRow').hidden = true;
        window.location.replace("admin.php");
    };

警报永远不会出现!

在一个表单中放置多个非提交按钮是否正确?

更新 我的表格看起来像这样:

<form action='save.php' method='POST' id='myForm'>

我已将行 document.getElementById('myForm').submit(); 添加到 getChange( ); 函数,使按钮为:

<input class='actionBtn' type='button' id='confirm' name='confirm' 
       value='Confirm' onclick='genChanged();'/>

但是,cancel(); 函数仍然不起作用!

最佳答案

id 和函数名称不能使用相同的名称。

在我的示例中,'a' 具有相同的 id 和函数名称 test(),而 b 具有不同的 id 和函数名称 cancel()...cancel 工作正常,而 test 则不行。

<script>
    function cancel(){
        alert('in cancel');
    };
    function test(){
        alert("in test")
    };
</script>

<body>
  <form action='save.php' method='POST' id='myForm'>
    <tr id='actionRow' name='actionRow' hidden='hidden'>
      <td>
        <input class='actionBtn' type='button' id='test' name='a' 
           value='a' onclick='test();'/>
      </td>
      <td>                                
        <input class='actionBtn' type='button' id='b' name='b' 
          value='b' onclick='cancel()' /> 
      </td>
   </tr> 
 </form>
</body>

有关更多信息,您可以查看这个有用的答案:https://stackoverflow.com/a/9160009/1318727

关于php - 一种表单中的常规按钮和提交按钮不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17800178/

相关文章:

javascript - 在 if 语句中添加 if 语句

javascript - React 中的数据管理策略

PHPMailer 不适用于 gmail

php - PHP解析/语法错误;以及如何解决它们

PHP mySQL 变量访问

javascript - 使用 JS 更改图像源不起作用

javascript - 在 JavaScript 中删除数组元素 - 删除与拼接

php - 如何对冒号和句点之间的文本使用 PHP preg_match?

javascript - 使用格式化数据渲染,使用 DataTables.net 中的原始数据排序

javascript - 在 Bootstrap 表单中单击添加行