如何使用jquery调用cakephp动作


How to call a cakephp action with jquery?

我只是想调用一个cakephp操作与jquery点击复选框。我想用$来刷新当前页面。get,因为我想把其他变量发送给动作。我尝试了以下代码:

HTML代码:
<?php $chkOpt = array('pertinence' => $pert, 'onClick' => 'refresh_current_page()', 'id' => 'matchCoche'); ?>
<td class="check-column"><?= $form->checkbox($waiting['ArgusWaitingsMobilesPrice']['brand'] . ';' . $waiting['ArgusWaitingsMobilesPrice']['mobile'] . ';' . $waiting['search']['RefAvatar']['mobile_id'], $chkOpt) ?></td>

Javascript代码:

<script type="text/javascript">
            function refresh_current_page() {
                $('#matchCoche').ready(function() {
                                $.get("<?php echo $this->here ?>");
                });
            }
        </script>

有人能帮我吗?

事先非常感谢您的帮助

我总是使用Html helper来制作url,让我省事。

<?php 
$this->Js->buffer('
    $(document).ready(function(){
        $('#matchCoche').click(function(){
              $.get("' . $this->Html->url( array('controller' => 'your_controller', 'action' => 'your_action', 'param1', 'param2') ) . '");
        });
    });
'); 
?>

假设你已经加载了'Js'助手,并且有:

<?php echo $this->Js->writeBuffer(); ?>

在结束正文标签

之前

我发现使用Js->buffer很方便。Js->writeBuffer将从缓冲区中收集所有的片段,并负责$(document)。准备功能。

最好使用click事件。不要和HTML混在一起:)

$(document).ready(function(){
    $('#matchCoche').click(function(){
          $.get("http://"+ document.domain +"/yourController/yourAction/param1/param2/");
    });
});
无论如何,如果你只是想用其他参数再次调用页面,为什么不使用一个普通的HTML链接呢?

正如前面的答案所提到的,您最好使用常规链接。您可以通过命名参数发送额外的变量:

<a href="/controller/action/id/variable1:value1/variable2:value2" />link</a>

相关文档可在:

http://book.cakephp.org/view/1096/Using-Helpers了解如何包含非默认帮助使用1.3. http://book.cakephp.org/view/1589/script。x Html帮助器来包含脚本使用1.2. http://book.cakephp.org/view/349/Methods。x Javascript helper

http://book.cakephp.org/1.3/en/The-Manual/Core-Helpers/Js.html