读取下拉javascript和rails中的选定值

时间:2017-03-09 12:10:00

标签: javascript ruby-on-rails

我使用的是rails 2.3.2 我试图铁路下拉选择。然后再次使用java脚本,rails控制器和模型类在同一页面中的另一个选择下拉列表中使用选定的下拉值。 我想得到" floor_id"值。但我没有得到" floor_id" rails view中的值。我可以得到" floor_id"视图页面中的值?

我将修改后的代码放在下面。

rails视图代码

<td>
<%= f.select(:floor_id, ProjectFloor.find(:all,:order => "name ASC",:conditions => ["project_id = ?", @project.id]).collect{|b|[b.name, b.id]}, {:include_blank => 'Select'},{:onchange => "select_floor_id(this.value);"}) %>
</td>

<td>
<%= f.select(:unit_id, @floor.collect{|b|[b.name, b.id]}, {:include_blank => 'Select'}) %>
</td>

模型类

def self.select_units(project_id,floor_id)

    self.find_by_sql(["SELECT distinct project_floor_units.* FROM `project_floor_units`
            INNER JOIN project_floors ON project_floors.id = project_floor_units.project_floor_id
            lEFT JOIN proposed_customers ON proposed_customers.unit_id = project_floor_units.id
            lEFT JOIN project_customers ON project_customers.project_floor_unit_id = project_floor_units.id
            WHERE project_floors.project_id = ? AND  project_floor_units.project_floor_id = ? AND
            proposed_customers.id IS NULL AND project_customers.id IS NULL
            GROUP BY project_floor_units.name ORDER BY project_floor_units.name ASC", project_id, floor_id])
  end

控制器

def new
    @floor_id = params[:floor_id]
    @project = Project.find(params[:project_id])
    @proposed_customer = ProposedCustomer.new
    @floor = ProjectFloorUnit.select_units(@project.id, @floor_id)

    respond_to do |format|
      format.html # index.html.erb
      format.js # index.html.erb
      format.xml  { render :xml => @proposed_customer }
    end
  end

这里的java脚本代码

<script type="text/javascript">

   function select_floor_id(floor_id) {
     alert(floor_id);
     jQuery.ajax({
      url: "/proposed_customers/new",
       type: "GET",
       dataType: "json",
       data: {"floor_id" : floor_id},
       success:function(data){

       }
     });
   }

</script>

感谢。

0 个答案:

没有答案