Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript JS switchery发送两个ajax请求,而不是一个_Javascript_Jquery_Ajax_Yii2 - Fatal编程技术网

Javascript JS switchery发送两个ajax请求,而不是一个

Javascript JS switchery发送两个ajax请求,而不是一个,javascript,jquery,ajax,yii2,Javascript,Jquery,Ajax,Yii2,我正在使用js switchery自定义我的复选框onchage复选框performajax request to the controller,这就是我如何使产品(比方说)处于活动或非活动状态。但现在我的复选框每次更改时都会发送两个请求,结果是我无法更改产品的状态(活动或未活动)。 阿贾克斯: 控制器操作: public function actionChangestatusactive() { if (isset($_POST['id']) and ! empty($_POS

我正在使用js switchery自定义我的复选框
onchage
复选框performajax request to the controller,这就是我如何使产品(比方说)处于活动或非活动状态。但现在我的复选框每次更改时都会发送两个请求,结果是我无法更改产品的状态(活动或未活动)。 阿贾克斯:

控制器操作:

public function actionChangestatusactive() {
        if (isset($_POST['id']) and ! empty($_POST['id'])) {
            $model = Product::findOne((int) $_POST['id']);
            if ($model) {
                if ($model->active == 1) {
                    $model->active = 2;
                } else {
                    $model->active = 1;
                }
                $model->update();
            }
        }
    }
我的模型功能:

public function changeActiveForm() {
        $active = "";
        if ($this->active == 1) {
            $active = 'checked="checked"';
        }
        return '<label class="switchery switchery-default taCenter">
                <input type="checkbox" class="js-switch" data-color="#99d683" data-secondary-color="#f96262" value="1" id="check_' . $this->id . '" name="field_types" ' . $active . ' onchange="changeStatusActive(' . $this->id . ', \'products/product\');"></input>
                <label data-off="' . Yii::t('app', 'Не') . '" data-on="' . Yii::t('app', 'Да') . '" for="check_' . $this->id . '"></label>
                <span></span>
            </label>';
    }
公共函数changeActiveForm(){
$active=“”;
如果($this->active==1){
$active='checked=“checked”';
}
返回'
';
}

在模型部分的哪里绑定onchange事件onchange=“changeStatusActive(“.$this->id.”,\'products/product\”)`
public function changeActiveForm() {
        $active = "";
        if ($this->active == 1) {
            $active = 'checked="checked"';
        }
        return '<label class="switchery switchery-default taCenter">
                <input type="checkbox" class="js-switch" data-color="#99d683" data-secondary-color="#f96262" value="1" id="check_' . $this->id . '" name="field_types" ' . $active . ' onchange="changeStatusActive(' . $this->id . ', \'products/product\');"></input>
                <label data-off="' . Yii::t('app', 'Не') . '" data-on="' . Yii::t('app', 'Да') . '" for="check_' . $this->id . '"></label>
                <span></span>
            </label>';
    }