I have two select
elements and one checkbox
. What i try to reach is: When i choose contry from the first one (Albania lets say) and press the checkbox
this options should load in the second select
. I am using kartiv Select2 widget
and somehow just can't get it how should i make it.
This my first select:
<?php
$mas[33] = Yii::t('app', 'Bulgaria');
$mas['----------------'] = ArrayHelper::map(Country::find()->all(), 'id', 'name');
?>
<?=
$form->field($model, 'country_id')->widget(Select2::classname(), [
'model' => $model,
'attribute' => 'country_id',
'data' => $mas,
'options' => [
'placeholder' => Yii::t('app', 'Избери държава'),
'class' => 'register-select',
],
'pluginOptions' => [
'allowClear' => true
],
])->label(false);
?>
And the second one:
<?php
$mas[0] = Yii::t('app', 'Главна страница');
$mas['----------------'] = ArrayHelper::map(Country::find()->all(), 'id', 'name');
?>
<?=
$form->field($model, 'address_country_id')->widget(Select2::classname(), [
'model' => $model,
'attribute' => 'address_country_id',
'data' => $mas,
'options' => [
'placeholder' => Yii::t('app', 'Избери държава'),
'class' => 'register-select',
],
'pluginOptions' => [
'allowClear' => true
],
])->label(false);
?>
And the js for the rest elements. They are simple inputs so i do not have problems whit them.
JS:
$(function () {
$('#fill_address').on('change', function () {
var country = $("input[name='register-form[country_id]']");//This is the first select
var city = $("input[name='register-form[city]']");
var address = $("input[name='register-form[delivery_address]']");
var post_code = $("input[name='register-form[post_code]']");
var country_delivery = $("input[name='register-form[address_country_id]']");//This is the second select which i should give the value from the first one
var city_delivery = $("input[name='register-form[address_city]']");
var address_delivery = $("input[name='register-form[address_address]']");
var postcode_delivery = $("input[name='register-form[address_post_code]']");
if($(this).is(":checked")){
city_delivery.val(city.val());
address_delivery.val(address.val());
postcode_delivery.val(post_code.val());
return false;
}
city_delivery.val('');
address_delivery.val('');
postcode_delivery.val('');
return false;
})
})
Thank you in advance!