2016-07-25 4 views
0

Select2ドロップダウンの選択値を設定します。私は住所と請求書のアドレスを含む2種類の住所を表示しています。 - テキストボックス Select2ドロップダウンで選択した値を設定します

  • 住所2 - テキストボックス
  • タウン - テキストボックス
  • 国 - 請求書の住所もこれらの同じフィールドがあり
  • ドロップダウン

    1. 住所1:各は、次のフィールドがあります。また、私は1つのチェックボックスを表示しています。クリックされた場合は、アドレスフィールド値を請求書アドレスフィールドにコピーする必要があります。 私は、テキストボックスの値をコピーし、別のテキストボックスに設定することができます。しかし、私はドロップダウン値をコピーすることができず、別のドロップダウンに設定することはできません。私は次のコードを使用しました:

      $('#invoiceAddress').on('ifChecked', function (event) { 
          $('input[data-cg-address]').each(function (index, element) { 
           var fieldname = $(this).data('cg-address'); 
           var fieldvalue = $(this).val(); 
           $('#invoice-address').find('input[data-invoice-address="' + fieldname + '"]').val(fieldvalue); 
          }); 
      
          $('select[data-cg-address]').each(function (index, element) { 
           var fieldname = $(this).data('cg-address'); 
           var selectedValue = $(this).val(); 
           $('#invoice-address').find('select[data-invoice-address="' + fieldname + '"]').val(selectedValue); 
          }); 
      }); 
      

      このコードは通常のドロップダウンであれば動作していました。 Select2ドロップダウンでは機能しません。

      私はこのコードをさらにいくつかのページで使用しました。だから私はそれらのドロップダウンのIDを使用することはできません。 私は共通のコードを書く必要があります。以下はフォームフィールドです。事前に

      <div class="panel"> 
                  <div class="panel-header"> 
                   <h3><i class="icon-bulb"></i> Address Details</h3> 
                  </div> 
                  <div id="cg-address" class="panel-content"> 
                   <div class="row"> 
                    <div class="col-sm-3"> 
                     <div class="form-group"> 
                      <label>@Html.DisplayNameFor(x => x.CompanyGroupAddress.Address1)</label> 
                      @Html.TextBoxFor(x => x.CompanyGroupAddress.Address1, new { @class = "form-control", data_cg_address = "address1" }) 
                      <div class="form-error"> 
                       @Html.ValidationMessageFor(x => x.CompanyGroupAddress.Address1) 
                      </div> 
                     </div> 
                    </div> 
                    <div class="col-sm-3"> 
                     <div class="form-group"> 
                      <label>@Html.DisplayNameFor(x => x.CompanyGroupAddress.Address2)</label> 
                      @Html.TextBoxFor(x => x.CompanyGroupAddress.Address2, new { @class = "form-control", data_cg_address = "address2" }) 
                      <div class="form-error"> 
                       @Html.ValidationMessageFor(x => x.CompanyGroupAddress.Address2) 
                      </div> 
                     </div> 
                    </div> 
                    <div class="col-sm-3"> 
                     <div class="form-group"> 
                      <label>@Html.DisplayNameFor(x => x.CompanyGroupAddress.Town)</label> 
                      @Html.TextBoxFor(x => x.CompanyGroupAddress.Town, new { @class = "form-control", data_cg_address = "town" }) 
                      <div class="form-error"> 
                       @Html.ValidationMessageFor(x => x.CompanyGroupAddress.Town) 
                      </div> 
                     </div> 
                    </div> 
                    <div class="col-sm-3"> 
                     <div class="form-group"> 
                      <label>@Html.DisplayNameFor(x => x.CompanyGroupAddress.Address4)</label> 
                      @Html.TextBoxFor(x => x.CompanyGroupAddress.Address4, new { @class = "form-control", data_cg_address = "address4" }) 
                      <div class="form-error"> 
                       @Html.ValidationMessageFor(x => x.CompanyGroupAddress.Address4) 
                      </div> 
                     </div> 
                    </div> 
                   </div> 
                   <div class="row"> 
                    <div class="col-sm-3"> 
                     <div class="form-group"> 
                      <label>@Html.DisplayNameFor(x => x.CompanyGroupAddress.Postcode)</label> 
                      @Html.TextBoxFor(x => x.CompanyGroupAddress.Postcode, new { @class = "form-control", data_cg_address = "postcode" }) 
                      <div class="form-error"> 
                       @Html.ValidationMessageFor(x => x.CompanyGroupAddress.Postcode) 
                      </div> 
                     </div> 
                    </div> 
                    <div class="col-sm-3"> 
                     <div class="form-group"> 
                      <label>@Html.DisplayNameFor(x => x.CompanyGroupAddress.CountyId)</label> 
                      @Html.DropDownListFor(x => x.CompanyGroupAddress.CountyId, new SelectList(Model.Counties, "Key", "Value"), "Select Company County", new { @class = "form-control", data_cg_address = "countyid" }) 
                      <div class="form-error"> 
                       @Html.ValidationMessageFor(x => x.CompanyGroupAddress.CountyId) 
                      </div> 
                     </div> 
                    </div> 
                    <div class="col-sm-3"> 
                     <div class="form-group"> 
                      <label>@Html.DisplayNameFor(x => x.CompanyGroupAddress.CountryId)</label> 
                      @Html.DropDownListFor(x => x.CompanyGroupAddress.CountryId, new SelectList(Model.Countries, "Key", "Value"), "Select Company Country", new { @class = "form-control", data_cg_address = "countryid" }) 
                      <div class="form-error"> 
                       @Html.ValidationMessageFor(x => x.CompanyGroupAddress.CountryId) 
                      </div> 
                     </div> 
                    </div> 
                   </div> 
                  </div> 
                 </div> 
                 <div id="invoice-address" class="panel"> 
                  <div class="panel-header"> 
                   <h3><i class="icon-bulb"></i>Invoice Address Details</h3> 
                  </div> 
                  <div class="panel-content"> 
                   <div class="row"> 
                    <div class="col-sm-3"> 
                     <div class="form-group"> 
      
                      <label> 
                       @Html.CheckBoxFor(x => x.InvoiceAddress, new { @id = "invoiceAddress", @for = "invoiceAddress", @class = "m-t-10" }) 
                       Same as Company Address 
                      </label> 
                     </div> 
                    </div> 
                   </div> 
                   <div class="row"> 
                    <div class="col-sm-3"> 
                     <div class="form-group"> 
                      <label>@Html.DisplayNameFor(x => x.CompanyGroupInvoiceAddress.Address1)</label> 
                      @Html.TextBoxFor(x => x.CompanyGroupInvoiceAddress.Address1, new { @class = "form-control", data_invoice_address = "address1" }) 
                      <div class="form-error"> 
                       @Html.ValidationMessageFor(x => x.CompanyGroupInvoiceAddress.Address1) 
                      </div> 
                     </div> 
                    </div> 
                    <div class="col-sm-3"> 
                     <div class="form-group"> 
                      <label>@Html.DisplayNameFor(x => x.CompanyGroupInvoiceAddress.Address2)</label> 
                      @Html.TextBoxFor(x => x.CompanyGroupInvoiceAddress.Address2, new { @class = "form-control", data_invoice_address = "address2" }) 
                      <div class="form-error"> 
                       @Html.ValidationMessageFor(x => x.CompanyGroupInvoiceAddress.Address2) 
                      </div> 
                     </div> 
                    </div> 
                    <div class="col-sm-3"> 
                     <div class="form-group"> 
                      <label>@Html.DisplayNameFor(x => x.CompanyGroupInvoiceAddress.Town)</label> 
                      @Html.TextBoxFor(x => x.CompanyGroupInvoiceAddress.Town, new { @class = "form-control", data_invoice_address = "town" }) 
                      <div class="form-error"> 
                       @Html.ValidationMessageFor(x => x.CompanyGroupInvoiceAddress.Town) 
                      </div> 
                     </div> 
                    </div> 
                    <div class="col-sm-3"> 
                     <div class="form-group"> 
                      <label>@Html.DisplayNameFor(x => x.CompanyGroupInvoiceAddress.Address4)</label> 
                      @Html.TextBoxFor(x => x.CompanyGroupInvoiceAddress.Address4, new { @class = "form-control", data_invoice_address = "address4" }) 
                      <div class="form-error"> 
                       @Html.ValidationMessageFor(x => x.CompanyGroupInvoiceAddress.Address4) 
                      </div> 
                     </div> 
                    </div> 
                   </div> 
                   <div class="row"> 
                    <div class="col-sm-3"> 
                     <div class="form-group"> 
                      <label>@Html.DisplayNameFor(x => x.CompanyGroupInvoiceAddress.Postcode)</label> 
                      @Html.TextBoxFor(x => x.CompanyGroupInvoiceAddress.Postcode, new { @class = "form-control", data_invoice_address = "postcode" }) 
                      <div class="form-error"> 
                       @Html.ValidationMessageFor(x => x.CompanyGroupInvoiceAddress.Postcode) 
                      </div> 
                     </div> 
                    </div> 
                    <div class="col-sm-3"> 
                     <div class="form-group"> 
                      <label>@Html.DisplayNameFor(x => x.CompanyGroupInvoiceAddress.CountyId)</label> 
                      @Html.DropDownListFor(x => x.CompanyGroupInvoiceAddress.CountyId, new SelectList(Model.Counties, "Key", "Value"), "Select Company County", new { @class = "form-control", data_invoice_address = "countyid" }) 
                      <div class="form-error"> 
                       @Html.ValidationMessageFor(x => x.CompanyGroupInvoiceAddress.CountyId) 
                      </div> 
                     </div> 
                    </div> 
                    <div class="col-sm-3"> 
                     <div class="form-group"> 
                      <label>@Html.DisplayNameFor(x => x.CompanyGroupInvoiceAddress.CountryId)</label> 
                      @Html.DropDownListFor(x => x.CompanyGroupInvoiceAddress.CountryId, new SelectList(Model.Countries, "Key", "Value"), "Select Company Country", new { @class = "form-control", data_invoice_address = "countryid" }) 
                      <div class="form-error"> 
                       @Html.ValidationMessageFor(x => x.CompanyGroupInvoiceAddress.CountryId) 
                      </div> 
                     </div> 
                    </div> 
                   </div> 
                  </div> 
                 </div> 
      

      おかげ

    答えて

    0

    これを試してみてください。

    $('#checkbox').click(function(){ 
        var dropdown1 = $('#dropdown1').val(); 
    
        $('#dropdown2').val(dropdown1); 
    }); 
    
    +0

    私はこのコードをさらにいくつかのページで使用しました。だから私はそれらのドロップダウンのIDを使用することはできません。私は共通のコードを書く必要があります。 – Lalitha

    0

    \t $("#chkbox").click(function() { 
     
         var options = $('#ddl1 option').clone(); 
     
         $('#ddl2').append(options); 
     
         });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
     
    <input type="checkbox" id="chkbox" /> 
     
    <select id="ddl1"> 
     
    <option value="1">One</option> 
     
    <option value="2">Two</option>    
     
    </select> 
     
    
     
    <select id="ddl2">

    +0

    私はいくつかのページでこのコードを使用しました。だから私はそれらのドロップダウンのIDを使用することはできません。私は共通のコードを書く必要があります。 – Lalitha

    +0

    与えられたコードが機能していません。あなたはそれを確認していただけますか? – Lalitha

    +0

    あなたは入力を書くことができます[type = 'checkbox'] – Developer

    0

    が、私は同じようにドロップダウンリストの設定値でトリガ( '変更')にコードを追加しました以下。

    $('#invoice-address').find('select[data-invoice-address="' + fieldname + '"]').val(selectedValue).trigger("change"); 
    

    これでうまくいきました。

    関連する問題