如何根据rails query ajax中的其他下拉列表值更改选择框值

问题描述:

这里我必须为User和UserType建模。如果我更改usertype然后用户属于特定的usertype将在下拉列表中列出。这是我的代码。

Here I have to model User and UserType. If I change the usertype then the user belongs to the particular usertype will be listed in the drop down. This is my code.

* .html

<div class="control-group string optional payslip_user_type_id">
    <label class="string optional control-label" for="payslip_user_type_id">Employee Type</label>
    <div class="controls">
        <%= f.collection_select :user_type_id, UserType.all, "id", "name", prompt: '--select--' %>
    </div>
</div>
<div class="control-group string optional payslip_user_id">
    <label class="string optional control-label" for="payslip_user_id">Employee Name</label>
    <div class="controls">
        <%= f.collection_select :user_id, @users, "id", "name", prompt: '--select--' %>    
    </div>
</div>

jquery文件是

$("#payslip_user_type_id").change(function(){
    var url = '/payslips/new?user_type_id=' + $(this).val() + ''
  $.ajax({
    url: url,
    dataType: 'script',
    type: 'GET',
    success: function(data){ $("#payslip_user_id").html(data); }
  });
})

控制器代码是

if params[:user_type_id].present?
      @users = UserType.find(params[:user_type_id]).users
      respond_to do |format|
        format.js 
      end
    end

和新的。 js.erb文件是

and the new.js.erb file is

<% if @users.present? %>
    <%= select_tag 'payslip_user_id', options_from_collection_for_select(@users, "id", "name") %>
<% else %>
    <%= select_tag 'payslip_user_id', '' %>
<% end %>

当ajax请求发出时,我可以在浏览器控制台中看到选择选项。但它在html中没有改变。请任何人帮帮我。

when ajax request made, I could see select options in browser console. But it is not changed in the html. Please any one help me.

尝试更改

dataType: 'script'

接受html

dataType: 'html'