I have a few select elements in my views that are supposed to fire after the user chooses an item from the dropdown.
My selects look like this:
<%= collection_select(:project, :id, projects, 'id', 'name', { },{:style => "width:150px", :onchange => "document.getElementById('project_btn').click()" }) %>
<span class="control_button_light" style="display:none;"><%= submit_tag 'jump_to_project', :id => "project_btn" %></span>
<%= observe_field("project_id", :frequency => 1, :function => "document.getElementById('project_btn').click()") %>
The problem is that the observe_field function is firing before the select loses focus. In other words, the submit element is "click开发者_运维技巧ed" 1 second after the select gets focus, even if the user hasn't finished choosing from the dropdown.
Anyone know how to delay the observer from clicking the submit until after the select loses focus?
Try this.
<%= observe_field("project_id", :frequency => 1, :function => "submit_it") %>
<script>
function sumbit_it{
setTimeout(function() { $('project_btn').click(); }, 1250);
}
</script>
After rereading the question, I don't think you need the observe_field, since you already have onchange for that field.
精彩评论