【发布时间】:2016-10-03 14:55:42
【问题描述】:
按下时我有一个链接,它通过渲染 ajax 从控制器请求页面,以前我只传递 id,但现在我想向控制器传递一个额外的参数,我如何实现这一点, 这是我尝试过的
这是仅将单个参数传递给控制器的链接
Html::a('click me', ['#'],
['value' => Url::to('checktruck?id='.$model->id), //this is where the param is passed
'id' => 'perform']);
这是需要 2 个参数的控制器代码:
public function actionChecktruck($id,$category) //it expects 2 parameters from above link
{
$truckdetails = Truck::find()->where(['id' =>$id])->one();
if (Yii::$app->request->post()) {
$checklistperform = new TodoTruckChecklist();
$truck = Truck::find()->where(['id'=>$id])->one();
$checklistperform->truck_id=$id;
$checklistperform->registered_by=Yii::$app->user->identity->id;
$checklistperform->save();
$truck->update();
var_dump($checklistperform->getErrors());
//var_dump($truck->getErrors());
}
else {
$truckcategory = Checklist::find()->where(['truck_category'=>$truckdetails->truck_category])->andWhere(['checklist_category'=>$category])->all();
return $this->renderAjax('truckyard/_checklistform', [
'truckcategory' => $truckcategory,'truckvalue'=>$id,
]);
}
}
这是我在发布请求期间依赖于上述控制器的另一个按钮的 jquery 代码
$("#postbutn").click(function(e) {
$.post("checktruck?id="+truckid, //here i would like to pass 2 params
{checked:checked,unchecked:unchecked,truckid:truckid}
)
}
这是没有post时的jquery代码
如何在链接中传递额外的参数,甚至是控制器的 $.post 请求
【问题讨论】:
标签: javascript jquery ajax yii