开发者

JQuery Rails 3: Unable to get JS executed with simple form

New with javascript and Rails. I've gotten some simple jQuery to work with my rails app when it's not a form.

Now I'm trying to do jQuery actions when the user hits 'Submit' in the form. The issue I have is that HTML code is executed instead of the scripts. BTW: Used scaffolding to create a simple form. It's the CREATE submit action I want to fetch.


application.js:

jQuery.ajaxSetup({
  'beforeSend': function(xhr) {
    xhr.setRequestHeader("Accept", "text/javascript")
  }
})

$(document).ready(function() {

  $("开发者_如何学编程#new_travel").submit(function() {
    $.post(this.action, $(this).serialize(), null, "script");
    return false;
  });

create.js.erb:

alert('Yeah!');  // Have more, but just want to see the jQuery to be executed

new.html.erb:

<%= form_for(@travel, :remote => true) do |f| %>
..
..
  <div class="actions">
    <%= f.submit %>

The travels controller holds the format.js in the respond_to block

Any clues as to what's wrong? Let me know if any information is missing.

Thanks!


SOLVED!

I found out the it did actually work for standard forms, but as the form I operated on was shows inside another page as part of another Jquery call the $("#new_travel") did not detect it.

I had to change that to: $("#new_travel").live('submit' function()...

And then it worked as all following #new_travel on the page got the javascript 'hook'.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜