javascript to display current options_for_select
I need to display the current selection for select table as a pop up box , where in index.rhtml I have this
<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) %>
</sele开发者_StackOverflow社区ct>
</p>
<% end %>
However, everytime I change the selection, nothing happens, and I don't see any errors/warning. Is there any additional lines that I have to add in the checkData function to show the current value of selection box?
Thank you
Your alert()
call should look like this:
var myTest = formid.table_id.options[formid.table_id.selectedIndex].value;
alert(myTest);
To attach the event handler and such, I'd restructure it a bit, like this:
<% 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>
精彩评论