-1

我有两个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;
        })
    })

先感谢您!

4

1 回答 1

0

假设您想在第一个 select2 字段的第二个字段中附加国家/地区,您可以执行此操作(选中复选框时的事件):

country_delivery.append('<option value="' + country.val() + '">' + country.text() + '</option>');
于 2017-10-20T05:50:39.513 回答