user2775888 user2775888 - 1 year ago 93
jQuery Question

jQuery is not working with my form in popover

I want to create a form inside a popover and use jQuery and Ajax to submit the form(do some calculation while submitting the form). But somehow jQuery is not working at all. I'm using ruby on rails.

Here is my code on the view:

%a.btn.btn-small{:id => "example", "data-toggle" => "popover", :type => "button"}
.head.hide Do Something
=form_for :object, action: '#', :html => { :class => "form", remote: true} do |c|
=c.number_field :var_1
=c.number_field :var_2
%buttion.btn.btn-default{id: "submit", type: "button"}Click

Here is my code in js and jQuery under app/assets/javascript,

$(function () {
html : true,
title: function () {
return $(this).parent().find('.head').html();
content: function () {
return $(this).parent().find('.content').html();

$('.form').submit(function(e) {
var datastring = $(this).serializeArray();
datastring.push({name:"post", value:"Post"});

var request = $.ajax({
type: "POST",
url: $(this).attr('action'),
data: datastring});
request.success(function(data) {

When I click "Click" button, there is even no alert(1) showing up, so i think the jQuery is not working but I can't work out what is going wrong?

Answer Source

Use this instead, this will bind the submit event in ajax loaded content

$('body').on('submit', '.form', function(){
  # your code