$('#category-id').empty();
for (i = 0; i < response[0].category.length; i++) {
$('#category-id').append($('<option></option>') //here typo mistake
.attr('value', response[0].category[i].id)
.attr('selected', 'selected')
.text(response[0].category[i].category_name));
}
这是来自Transaction View中的JavaScript代码_ form.php。它是通过单选按钮单击触发的。 Select2包含收入和支出类别。当我选择一个收入类别并单击费用单选按钮时。该选项更改了费用类别。但是所选收入类别仍然显示。不清楚。
<?=
$form->field($model, 'category_id')->widget(Select2::classname(), [
'data' => Category::getCategoryListByType($model->transaction_type),
'options' => [
'id' => 'category-id',
'placeholder' => Yii::t('frontend/common', 'Please select one'),
'allowClear' => true,
],
'pluginOptions' => ['allowClear' => true]
]);
?>
Select2 php代码。请帮忙。谢谢。
这对我有用-
$('#accessories').val('').trigger("change");
'#accessories'是select2的ID