简体   繁体   中英

Rails 3 Submit Radio Button Form with Jquery Remotely (without using $.post)?

I'm wondering if there is a way to submit a form of radio buttons with jQuery. The problem I'm having is that I'm applying a start rating jQuery plugin to a rails form, and this plugin has it's own click callback. I'd like to simply tell the rails form to submit from within this callback.

Here is the form in rails:

<%= form_for([@contact, @contact.overall_ratings.new],
                :remote => true, 
                :format => :js) do |f| %>
    <%-# TODO set these up in a loop %>
    <%= f.radio_button :rating, 1, {:class => "star"} %>
    <%= f.radio_button :rating, 2, {:class => "star"} %>
    <%= f.radio_button :rating, 3, {:class => "star"} %>
    <%= f.radio_button :rating, 4, {:class => "star"} %>
    <%= f.radio_button :rating, 5, {:class => "star"} %>

<% end %>

Here is the call to the plugin, plus the callback:

$(function(){ 

    $('input.star').rating({
            callback: function(value, link){
                //get parent form
                var form = $(this).parent();
                $(form).submit();

            }
    });

});

The problem with what I've got so far is that it's redirecting to the .js file. Seems like it doesn't obey the forms call to :remote => true, which doesn't surprise me because I'm calling it from the plugin callback.

Thanks for the help!

What about

$('input.star').click(function(){ $(this).parent().submit(); return true; });

? This will trigger form submit after setting the value of radio input, so it will work anyway.

Also - are your sure jquery_ujs is properly referenced from application.js? Because it seems, that this form is not set as remote.

And finally - :format=>:js is not needed here IMO, as format is taken also from headers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM