Yii2 Select2 kartik-v在复选框更改

时间:2017-10-19 14:32:41

标签: jquery yii2 kartik-v

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

提前谢谢!

1 个答案:

答案 0 :(得分:0)

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

country_delivery.append('<option value="' + country.val() + '">' + country.text() + '</option>');