2017-10-19 89 views
-1

我有兩個select元素和一個checkbox。我試圖達到的是:當我從第一個選擇contry(阿爾巴尼亞讓說),並按checkbox這個選項應該加載在第二個select。我正在使用kartiv Select2 widget,不知何故我無法得到它,我應該怎麼做。 這是我的第一選擇:Yii2 Select2 kartik-v給出複選框更改的選擇值

<?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; 
     }) 
    }) 

預先感謝您!

回答

0

假設你希望從第一選擇2場附加在第二場中的國家,你可以(在複選框被選中的事件),這樣做:

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