Rails 3 Submitting a form with Javascript causes html response instead of a AJAX response
When I submit a form with Javascript I get the html response, it is like it ignores the remote true parameter. If I submit the form with the submit button it does the AJAX and updates the page elements requested. So the only difference in the code is the presence of the onchange
parameter and the button. Here is my code for the form:
<% form_tag('switch_car', :method => :put, :name => "switch_car", :remote => true) do %>
<div class="field">
<label>Car Name:</label>
<%= select_tag(:id, options_from_collection_for_select(active_cars, "id", "name"), :onchange => "submitForm('switch_car')", :include_blank => true)%>
</div>
<% end %>
Here is my Javascript function: just a simple submit.
<script type="text/javascript" charset="UTF-8">
function submitForm(formname){
document.switch_car.submit();
}
</script>
Here is the HTML generated:
<form accept-charset="UTF-8" action="switch_car" data-remote="true" method="post" name="switch_car"><div style="margin:0;padding:0;display:inline"开发者_运维技巧;><input name="utf8" type="hidden" value="✓" /><input name="_method" type="hidden" value="put" /><input name="authenticity_token" type="hidden" value="PEbdqAoiik37lcoP4+v+dakpYxdpMkSm7Ub8eZpdF9I=" /></div><div class="field">
<label>Car Name:</label>
<select id="id" name="id" onchange="submitForm('switch_car')"><option value=""></option><option value="9">Truck</option>
<option value="10">Car</option></select>
</div>
Anybody know why the Javascript submit causes a different result than the button on the same form? If so can I get the Javascript submit to work with an Ajax result?
Found the issue. I thought submitting the form with javascript would submit the form with the same remote => true option but it appears to bypass that. I had to submit using new AjaxRequest to get it working.
精彩评论