我在尝试在yii2的onchange中传递两个变量时遇到麻烦。下面的代码不起作用。
_form
<?= $form->field($model, 'commodity')->dropDownList(
ArrayHelper::map(['empty'=>'Empty string'], 'id', 'value'),
[
'prompt'=>'------- Select --------',
'id'=>'pcommodity',
'disabled'=>"disabled",
'style' => 'width:250px',
'onchange'=>
'$.post("/import-conditions/plants/listsspecies?
name='.'" +$(this).val(),function(data)
{
$( "select#pspecies").html(data)
})
here is the problem
**$.post("/import-conditions/plants/listsintendeduse?name='.'"
+$(this).val(),function(data)'.'+$(category).val(),function(data)
{
$( "select#pintendeduse").html(data)
})**
$.post("/import-conditions/plants/listsorigin?name='.'"
+$(this).val(),function(data)
{
$( "select#porigin").html(data)
})
;'
])->label(false);?>
此表单信息将传递给控制器,这是控制器的代码。
public function actionListsintendeduse($name,$category)
{
$countMaindata= Plants::find()
->where(['commodity'=> $name,'category'=>$category])
->count();
$maindata = Plants::find()
->select('intendeduse')
->where(['commodity'=> $name,'category'=>$category])
->orderBy(['intendeduse'=>SORT_ASC])
->distinct()
->all();
if($countMaindata > 0)
{
// echo '<option value="">Select intendeduse </option>';
foreach ($maindata as $main)
{
echo "<option value='".$main->intendeduse."'> ".$main->intendeduse."</option>";
}
}else{
echo "<option> - </option>";
}
}
我正在尝试传递两个变量,并让控制器按这两个变量进行过滤。
答案 0 :(得分:1)
您遇到语法错误
尝试
$.post(
"/import-conditions/plants/listsintendeduse?name="
+$(this).val() + "&category=" + $(category).val(),
function(data) {
$( "select#pintendeduse").html(data)
}
)