Javascript JS switchery发送两个ajax请求,而不是一个
我正在使用js switchery自定义我的复选框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
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>';
}