我想提交一個使用jQuery和Ajax的Rails表單,但它不工作,我不知道爲什麼。我知道有remote: true
使用AJAX做事情,但這並不足以解決我的問題,因爲我想在單擊提交按鈕時執行其他jQuery操作,並對操作狀態(即成功和失敗)提供良好的反饋。這是代碼我目前有以下形式:使用jQuery提交Rails表單
<%= 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
});
});
我想在這裏工作過的例子Submit form in rails 3 in an ajax way (with jQuery)
我已經沒有提交按鈕,是原因它不工作?任何幫助,將不勝感激。
您能指定什麼工作不正確 – krichard
那麼按鈕的點擊工作,然而之後沒有任何反應。我認爲提交不起作用。即使我註釋了提交內容代碼,它也不記錄任何內容。 – Hugs
我的不良欄杆代碼:/表單的ID未設置。需要html:{id:「mail-form」} – Hugs