用于显示当前选项的javascript

用于显示当前选项的javascript,javascript,ruby-on-rails,Javascript,Ruby On Rails,我需要将选择表的当前选择显示为弹出框,在index.rhtml中有 <SCRIPT LANGUAGE="JavaScript"> function checkData() { var myTest = formid.table_id.options[formid.table_id.selectedIndex].value; alert 'myTest'; } </script> <% form_tag :controller => 'project_cont

我需要将选择表的当前选择显示为弹出框,在index.rhtml中有

<SCRIPT LANGUAGE="JavaScript">
function checkData()
{
var myTest = formid.table_id.options[formid.table_id.selectedIndex].value;
alert 'myTest';
}
</script>

<% form_tag :controller => 'project_controller', :action => 'actionfor_menu', :id=>'formid' do %>    
      <p>
      <select id="table_id" name="table_id" size="9">
        <%= options_from_collection_for_select(@monitors, 'id', 'name', @monitors.first.id) %>  
      </select>
      </p>
<% end %>

函数checkData()
{
var myTest=formid.table\u id.options[formid.table\u id.selectedIndex].value;
警报“我的测试”;
}
'project\u controller',:action=>'actionfor\u menu',:id=>'formid'do%>

但是,每次我更改选择时,都不会发生任何事情,也不会看到任何错误/警告。是否需要在checkData函数中添加其他行以显示选择框的当前值

谢谢你

你的电话应该是这样的:

var myTest = formid.table_id.options[formid.table_id.selectedIndex].value;
alert(myTest);
<% form_tag :controller => 'project_controller', :action => 'actionfor_menu', :id=>'formid' do %>    
  <p>
    <select id="table_id" name="table_id" size="9">
      <%= options_from_collection_for_select(@monitors, 'id', 'name', @monitors.first.id) %>  
    </select>
  </p>
<% end %>
<script type="text/javascript">
  document.getElementById("table_id").onchange = function () {
    alert(this.options[this.selectedIndex].value);
  };
</script>
要附加事件处理程序等,我将对其进行一些重构,如下所示:

var myTest = formid.table_id.options[formid.table_id.selectedIndex].value;
alert(myTest);
<% form_tag :controller => 'project_controller', :action => 'actionfor_menu', :id=>'formid' do %>    
  <p>
    <select id="table_id" name="table_id" size="9">
      <%= options_from_collection_for_select(@monitors, 'id', 'name', @monitors.first.id) %>  
    </select>
  </p>
<% end %>
<script type="text/javascript">
  document.getElementById("table_id").onchange = function () {
    alert(this.options[this.selectedIndex].value);
  };
</script>
'project\u controller',:action=>'actionfor\u menu',:id=>'formid'do%>

document.getElementById(“table_id”).onchange=function(){ 警报(this.options[this.selectedIndex].value); };