3

在这里,我必须对 User 和 UserType 进行建模。如果我更改用户类型,那么属于特定用户类型的用户将列在下拉列表中。这是我的代码。

*.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

并且 new.js.erb 文件是

<% 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中没有改变。请任何人帮助我。请参阅下面的屏幕截图

4

1 回答 1

5

尝试改变

dataType: 'script'

接受 html

dataType: 'html'
于 2013-07-24T05:27:17.430 回答