Cakephp生成的javascript在onclick上抛出错误

Cakephp generated javascript throws error onclick

本文关键字:出错 错误 onclick javascript Cakephp      更新时间:2023-09-26

我的系统中有一组测试。测试可能会暂停。在我的测试控制器的索引页上,我使用以下代码来显示测试。

<table>
<tr><th>ID</th><th>Name</th><th>Updated</th><th>Actions</th></tr>
<?php foreach ($tests as $test): ?>
    <tr>    
    <td>
        <?php echo $test['Test']['id']; ?>
    </td>
    <td class="actions">
        <?php
            $status = ($test['Test']['is_paused'] == 1) ? 'Un-pause' : 'Pause';
            echo $this->Form->postLink($status, array('controller'=>'tests', 'action' => 'pause', $test['Test']['id'], 'admin' => 1), array('confirm'=>'Are you sure?') );
        ?>
        <?php 
            echo $this->Html->link('Edit', array('controller'=>'tests', 'action' => 'edit', 'admin'=>1, $test['Test']['id']));
        ?>
        <?php 
            echo $this->Form->postLink('Delete', array('controller'=>'tests', 'action' => 'delete',       $test['Test']['id'], 'admin'=>1), array('confirm'=>'Are you sure?') );
        ?>
    </td>
</tr>
<?php endforeach; ?>
</table>

这将生成一个测试列表,并使用来自cakehp-form-helper的postlink为每个测试提供一些操作函数。造成问题的是暂停按钮。有时单击时会引发以下错误。

Uncaught TypeError: Object #<HTMLCollection> has no method 'submit' 

第一次单击暂停时几乎不会出现此错误。可以切换暂停,因此此错误通常在切换暂停几次后弹出。我对我的JS不是很了解,因为JS是自动构建的,所以我不知道如何解决这个问题。当这个问题发生时,暂停按钮将不会起任何作用,我甚至不知道从哪里开始调试。感谢所有提供帮助的人。

UPDATE:这是postlink:在浏览器中呈现的html代码

<form action="/admin/tests/pause/5" name="post_521370eb05d3f" id="post_521370eb05d3f" style="display:none;" method="post">
    <input type="hidden" name="_method" value="POST">
</form>
<a href="#" onclick="if (confirm('Are you sure?')) { document.post_521370eb05d3f.submit(); } event.returnValue = false; return false;">Un-pause</a> 

代码document.post_521370eb05d3f.submit();似乎是问题所在,如果非要我猜测的话,我认为出于某种原因,document.post_521370eb05d3f没有提交方法。不过我不知道该怎么解决。

$this->Html->link(__('Some Text to Click'), array(
    'admin' => true,
    'action' => 'delete',
    $delete_id,
), null, 'Are you sure?'));
相关文章: