如果标记了复选框,CakePhp 2会阻止ajax

问题描述 投票:0回答:1

我需要标记一个名为brandId的复选框,

<input type="checkbox" name="lastBrandId" id="lastBrandId" value="1">

当我勾选复选框时,我会触发另一个jQuery来恢复并填充序列号字段:

<input name="data[Brand][serialNumber]" type="text" id="serialNumber">

如果选中该复选框,我不希望触发下面的cakephp 2 get方法:

   echo $this->Js->get('#BrandId')->event('change', $this->Js->request(
        $ajaxUrl,
        array(
            'update' => '#form-ajax',
            'evalScripts' => true,
            'before'=>"$('#loading').show();",
            'complete'=>"$('#loading').hide()",
            'async' => true,
            'method' => 'post',
            'data' => $this->Js->serializeForm(array('isForm' => false, 'inline' => true)),
            'dataExpression' => true,
        )
    ));

在这种情况下,我无法看到我可以使用哪个变量。我可以用JavaScript代码附上php代码吗?

我尝试包含cakephp ajax请求的php代码,但没有停止ajax请求:

if(!empty($this-request-data['lastBrandId']))

还有其他方法吗?

jquery ajax cakephp-2.3
1个回答
1
投票

我无法测试它,但也许你可以使用(格式化以查看逻辑):

'before'=>"
if ($(this).is(':checked')) {
   return false;
} else {
   $('#loading').show();
   return true;
}",

这个想法是return false应该停止ajax调用。

我个人不再使用JsHelper了。外部.js文件更容易调试,可以缩小。

© www.soinside.com 2019 - 2024. All rights reserved.