Javascript jQuery无法在popover中使用我的表单
我想在popover中创建一个表单,并使用jQuery和Ajax提交表单(在提交表单时进行一些计算)。但不知何故,jQuery根本不起作用。我正在使用ruby on rails 以下是我在视图中的代码:Javascript jQuery无法在popover中使用我的表单,javascript,jquery,ruby-on-rails,ajax,twitter-bootstrap,Javascript,Jquery,Ruby On Rails,Ajax,Twitter Bootstrap,我想在popover中创建一个表单,并使用jQuery和Ajax提交表单(在提交表单时进行一些计算)。但不知何故,jQuery根本不起作用。我正在使用ruby on rails 以下是我在视图中的代码: %a.btn.btn-small{:id => "example", "data-toggle" => "popover", :type => "button"} %i.icon-calendar .head.hide Do So
%a.btn.btn-small{:id => "example", "data-toggle" => "popover", :type => "button"}
%i.icon-calendar
.head.hide Do Something
.content.hide
=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
下面是我在app/assets/javascript下的js和jQuery代码
$(function () {
$('#example').popover({
html : true,
title: function () {
return $(this).parent().find('.head').html();
},
content: function () {
return $(this).parent().find('.content').html();
}
}).popover('show');
});
$(function(){
$('.form').submit(function(e) {
alert(1);
e.preventDefault()
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) {
console.log(data);
})
});
});
当我单击“click”按钮时,甚至没有显示任何警报(1),因此我认为jQuery不起作用,但我无法找出哪里出了问题 改用它,它将绑定ajax加载内容中的提交事件
$('body').on('submit', '.form', function(){
# your code
});
如果您遵循最佳实践,那么您应该像这样编写jQuery:
$(document).ready(function(){
$('body').on('submit', '.form', function(){
// your code
});
});
是否可以发布HTML代码而不是服务器模板?在控制台中附加提交事件之前,请检查$('.form')是否存在。