简体   繁体   English

javascript绑定另一个select2输入字段rails的select2输入onchange事件的数据

[英]javascript bind data of select2 input onchange event of another select2 input field rails

Ok i have two select2 input fields and when i change the value of the first select2 input field, i want to be able to trigger the data that shud be in the 2nd select2 input field. 好的,我有两个select2输入字段,当我更改第一个select2输入字段的值时,我希望能够触发第二个select2输入字段中出现的数据。

this is my two select2 fields in index.html.erb 这是我在index.html.erb中的两个select2字段

  <%= f.input_field :lender_id, collection: Lender.all.pluck(:code, :id), class: 'form-control legal-service lender_field_code', prompt: 'Please Select Banker', disabled: (f.object.new_record? ? false : true ) %>

        <%= f.input_field :banker_id, collection: Banker.joins(:lenders).where("locked_at is null AND approved = true AND organizations.code = ?", params[:lender_id_code]).order(name: :asc), class: 'form-control legal-service lender_field_name ', prompt: 'Please Select', id: "banker_id" %>

the first select2 input field has a list of codes eg ('AFG') and the 2nd select2 has a list of names 'Someone name'. 第一个select2输入字段具有一个代码列表,例如('AFG'),第二个select2输入名称具有一个列表'Someone name'。

I already have the code to call the names only under the specific code but i need my code my page to trigger the response. 我已经有了仅在特定代码下才能调用名称的代码,但是我需要我的代码页面才能触发响应。 So when i select AFG only the names relating to AFG shud show up 因此,当我选择AFG时,只会显示与AFG shud相关的名称

in my javascript file i have the following code 在我的javascript文件中,我有以下代码

$.get('/loan_applications/_lawyer_fields', function(data) { 
      $('.lender_field_name').html(data);
 });

Try this 尝试这个

$('.lender_field_code select').bind('change',function () {    
    $(this).parents('.lender_field_code').find('.lender_field_name').text($(this).fi‌​nd('option: select').text()); 
})

fiddle link 小提琴链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM