开发者

AJAX rails check box using javascript .submit()

开发者 https://www.devze.com 2023-04-05 14:36 出处:网络
I\'m using Rails 3.1 and I need a checkbox to toggle and save to the database without the page refreshing or redirecting.

I'm using Rails 3.1 and I need a checkbox to toggle and save to the database without the page refreshing or redirecting.

Let me preface by saying i'm fairly novice, if i'm coming at this problem from the wrong angle please let me know.

Here is my form:

<% form_for book, :remote => true do |f| %>
   <%= f.check_box :finished %>
<% end %>

Here开发者_如何学编程 is my jQuery javascript:

$('input#book_finished').live("click", function() {
    $(this).parent('form').submit();
});

The :remote = > true doesn't work when I submit via jQuery, but does if I throw in a submit button (which I can't have). How can I submit on clicking the checkbox though JS?


Your code so far only handles the submitting via a click on the checkbox. In order to submit the form via AJAX, you'll still have to catch the submit event, prevent the default behavior and send the form data:

$('#form-id').submit(function(){
  $.ajax({
    type: this.method,
    url: this.action,
    data: $(this).serialize(),
    success: function(){
      ... // do something in case everything went find
    }
  });
  return false;
});
0

精彩评论

暂无评论...
验证码 换一张
取 消