我有两个select
元素和一个checkbox
. 我试图达到的是:当我从第一个中选择 contry(让我们说阿尔巴尼亚)并按下checkbox
此选项时,应该在第二个中加载select
。我正在使用kartiv Select2 widget
,但不知怎的,我应该怎么做。这是我的第一个选择:
<?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);
?>
第二个:
<?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);
?>
其余元素的js。它们是简单的输入,所以我对它们没有任何问题。
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;
})
})
先感谢您!