使用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: "选择间隔>时间" %> <%= select_tag "col", options_for_select(@ccs), prompt: "选择列数" %> <%= 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 %>