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

使用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 %>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值