0

我正在使用带有验证规则的动态表单,我想让浏览器在提交之前也验证表单,所以我使用了whenClient. 有一个复选框,单击时不会处理验证...

['dv_number', 'required', 'when' => function ($model) {
        return $model->is_cancelled == 1;
        }, 'whenClient' => "function (attribute, value) {  
            console.log(attribute.name);
            var firstletter=(attribute.name.charAt(0));
            var index='';
            if(firstletter==='['){ //when dynamic form is not activated and name of inputs is like [0]dv_number
                index=attribute.name.charAt(1);//I get the array index
            }else{
                index=attribute.name.charAt(9);//dynamic form activated the name of inputs changed like this TblDvBub[0][dv_number].
            }

            if($('[name=\'TblDvBub[0][is_cancelled][]\']').is(\":checked\"))
            {
                return false;//validation will not take place
            }
            else
            {
                return true;//validation from browser takes place
            }

        }"
    ],

所以我使用index变量获取索引号。我的问题是替换0这一行if($('[name=\'TblDvBub[0][is_cancelled][]\']').is(\":checked\"))中的索引并使其像这样if($('[name=\'TblDvBub[index][is_cancelled][]\']').is(\":checked\"))

请注意,从客户端被" "...包围时开始的整个代码

我已经尝试过这个和很多其他的,但无济于事。

if($('[name=\'TblDvBub[' + index +'][is_cancelled][]\']').is(\":checked\"))

在此处输入图像描述

4

1 回答 1

1

这只是一个 JS/jQuery/PHP 问题。您必须在选择器的名称过滤器中转义括号:

if($('[name=TblDvBub\\[' + index + '\\]\\[is_cancelled\\]\\[\\]]').is(\":checked\"))

不太确定,但是当它在 PHP 中的 "" 字符串中时,您甚至必须将转义字符加倍:

if($('[name=TblDvBub\\\\[' + index + '\\\\]\\\\[is_cancelled\\\\]\\\\[\\\\]]').is(\":checked\"))

我认为这是这里的问题。它有帮助吗?第一种方法还是第二种方法正确?

请参阅jQuery 文档(在开始段落中注明)。

于 2015-06-24T08:37:11.087 回答