![](/img/trans.png)
[英]Rails + jQuery - Using jQuery to submit the form w/o the Submit button
[英]Submit Rails form using jQuery
我正在尝试使用jQuery和Ajax提交Rails表单,但它不起作用,我不知道为什么。 我意识到有remote: true
使用AJAX做事情是remote: true
,但这在我的情况下还不够,因为我想在点击提交按钮时执行其他jQuery操作,并对动作的状态即成功和失败给出良好的反馈。 这是我目前有以下形式的代码:
<%= form_for @mail, id: "mail-form", url: subscription_mail_create_recipients_path(@mail) do |f| %>
<strong>Choose the users you want to target..</strong><br/>
<%= f.check_box :target_users %> Users
<div class="actions">
<a id="save-config" class="btn btn-primary purple-button">Save configuration</a>
</div>
<% end %>
jQuery:
$('#save-config').click(function(){
$('#mail-form').submit(function() {
var valuesToSubmit = $(this).serialize();
console.log(valueToSubmit);
$.ajax({
url: $(this).attr('action'), //sumbits it to the given url of the form
data: valuesToSubmit,
dataType: "JSON" // you want a difference between normal and ajax-calls, and json is standard
}).success(function(json){
//act on result.
});
alert("method failed");
return false; // prevents normal behaviour
});
});
我试图在这里解决这个例子在一个ajax方式中使用rails 3提交表单(使用jQuery)
我没有提交按钮,是不是因为它不工作? 任何帮助将不胜感激。
您的代码确实定义了在提交表单时要执行的函数。 它会对按钮上的每次点击执行此操作。 但你想要的是每次点击提交表格的内容。 请尝试以下方法:
$('#save-config').click(function(){
var valuesToSubmit = $('#mail-form').serialize();
console.log(valueToSubmit);
$.ajax({
url: $('#mail-form').attr('action'), //sumbits it to the given url of the form
data: valuesToSubmit,
dataType: "JSON" // you want a difference between normal and ajax-calls, and json is standard
}).success(function(json){
//act on result.
});
return false; // prevents normal behaviour
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.