码迷,mamicode.com
首页 > 其他好文 > 详细

select_tag 选择后自动提交,并且保持选择的项

时间:2014-10-29 01:51:10      阅读:315      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   color   ar   使用   for   sp   

使用select来选择,,,选择就响应真是花了好大的功夫。。。。你妹。。。在controller设置变量来储存选择后的值。。。js中如果select选择为空那么传入的值是””而不是null

selectedIndex的值都是数字,从0开始,而不是option的值

controller

#sdn的网络流量监控
  def monitor
    @vms = VirtualMachine.owned_by(current_user).select(*).joins("left join vnets on virtual_machines.vnet_id = vnets.id").where("vnets.net_type = ‘sdn‘")
    @switches = [["second","_10seconds"],["minute","_5minutes"],["hour","_hour"],["day","_day"],["week","_week"],["month","_month"],["year","_year"]]
    @ccs = [["1","1"],["2","2"],["3","3"],["4","4"],["5","5"],["6","6"],["7","7"],["8","8"],["9","9"],["10","10"]]
    @jiange_value = params[:times]
    @col_value = params[:col]
    @lscol = @col_value.to_i

    id_hash = Hash.new 
    id_hash["_10seconds"] = "1"
    id_hash["_5minutes"] = "2"
    id_hash["_hour"] = "3"
    id_hash["_day"] = "4"
    id_hash["_week"] = "5"
    id_hash["_month"] = "6"
    id_hash["_year"] = "7"
    @lsjiange = id_hash[@jiange_value]
  End

 

monitor.html.erb

<%= render partial: vnet_nav_bar %>
<meta charset="utf-8">
<head>
  <script>
    window.onload = initForm;
    function initForm()
    {
      //alert("<%= @lsjiange %>");
      document.getElementById(col).selectedIndex = <%= @lscol %>
      document.getElementById(times).selectedIndex = <%= @lsjiange %>
      document.getElementById("col").onchange = popu;
      document.getElementById("times").onchange = popu;
    }
    
    function popu()
    {
        //alert("bb");
        var form1 = document.getElementById("idform");
        form1.submit();

        var c = document.getElementById(col).selectedIndex;
        //alert(c);
        
    }
  </script>
</head>
<%= form_tag("monitor",:id => "idform",name: "idform") do |f| %>
  <%= select_tag "times", options_for_select(@switches),prompt: "选择间隔>时间" %>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  <%= select_tag "col", options_for_select(@ccs), prompt: "选择列数" %>
  &nbsp;&nbsp;&nbsp;
  <%= submit_tag("刷新") %>

  <% columns = @col_value.to_i %>
  <% if columns == 0 %>
  <% columns = 2 %>

  <% if columns == 0 %>
  <% columns = 2 %>
  <% end %>

  <% if @jiange_value == "" %>
  <% @jiange_value = "_5minutes"%>
  <% end %>

  <% rows = @vms.size/columns %>
  <% unless (@vms.size%columns)==0 %>
  <% rows = rows + 1 %>
  <% end %>
  <% vmid = 0 %>

  <center>
    <table>
        <% for i in 0..rows-1 do %>
          <tr>
          <% for j in 0..columns-1 do %>
            <td>
            <% if @jiange_value.nil? %>
            <% @jiange_value = _5minutes %>
            <% end %>
            <%= image_tag("rrdtool/#{@vms[vmid].uuid}#{@jiange_value}.png") %>
            <% vmid = vmid + 1 %>
            </td>
            <% if vmid >= @vms.size %>
            <% break %>
            <% end %>
          <% end %>
          </tr>
        <% end %>
    </table>
  </center>
<% end %>

 bubuko.com,布布扣

select_tag 选择后自动提交,并且保持选择的项

标签:style   blog   http   io   color   ar   使用   for   sp   

原文地址:http://www.cnblogs.com/juandx/p/4058399.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!