【问题标题】:CakePhp 2 prevent ajax if checkbox is marked如果复选框被标记,CakePhp 2 会阻止 ajax
【发布时间】:2018-09-13 16:28:35
【问题描述】:

我需要标记一个名为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】:

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

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

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

    我个人不再使用 JsHelper。外部 .js 文件更易于调试并且可以缩小。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-03
      • 2016-04-26
      • 2011-09-19
      • 2019-12-03
      • 2016-03-14
      相关资源
      最近更新 更多