1

我有一个从 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();'/>

然而,取消();功能还是不行!

4

1 回答 1

1

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

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

<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

于 2013-08-25T00:43:16.113 回答