【问题标题】:How to call a cakephp action with jquery?如何用 jquery 调用 cakephp 动作?
【发布时间】:2013-03-13 10:14:35
【问题描述】:

我只想用 jquery 点击复选框来调用 cakephp 动作。我只想通过我想使用 $.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>

有人可以帮我吗?

非常感谢您的帮助。

【问题讨论】:

    标签: php javascript cakephp get


    【解决方案1】:

    我总是使用 Html helper 来制作网址,让我免于麻烦。

    <?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 将从缓冲区中收集所有的 sn-ps 并处理 $(document).ready 函数。

    【讨论】:

      【解决方案2】:

      最好使用click 事件。不要将它与 HTML 混合 :)

      $(document).ready(function(){
          $('#matchCoche').click(function(){
                $.get("http://"+ document.domain +"/yourController/yourAction/param1/param2/");
          });
      });
      

      无论如何,如果你只是想用其他参数再次调用页面,为什么不使用普通的HTML链接呢?

      【讨论】:

        【解决方案3】:

        如上一个答案中所述,您最好只使用常规链接。您可以通过命名参数发送其他变量:

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

        【讨论】:

          【解决方案4】:

          相关文档可以在以下位置找到:

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

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

          【讨论】:

            猜你喜欢
            • 2017-02-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-09-12
            • 1970-01-01
            相关资源
            最近更新 更多