当用户从日期选择器弹出窗口中选择日期(或对此进行任何更改,无论是日期还是时间)时,我想自动保存表单。在 Rails 中,我使用带有 :remote => true
的表单来简单地更改截止日期属性。这只是一个字段,因此让用户在选择日期后单击“提交”按钮感觉有点过分了。我目前已将其设置为在用户更新表单后使用 AJAX 显示更新日期,但同样,我希望无需实际单击提交按钮即可完成此操作。
这是我的 erb:
<%= form_for todo, :remote => true, :url => update_todo_project_path, :html => {:id => "todo#{todo.id}" } do |f| %>
<%= f.hidden_field :id %>
<%= f.text_field :due %>
<%= f.submit %>
<% end %>
Javascript(只是一个外部js文件):
$('input#project_todo_due').datetimepicker({
dateFormat: 'yy-mm-dd',
timeFormat: 'hh:mm'
});
和 update_todo.js.erb:
$("#dueon<%= @todo.id%>").html("<%= raw escape_javascript(render(:partial => 'duedate')) %>");
最佳答案
我要尝试一下:
$('input#project_todo_due').datetimepicker({
dateFormat: 'yy-mm-dd',
timeFormat: 'hh:mm',
onClose: function(strDate, datepicker) {
// According to the docs this situation occurs when
// the dialog closes without the user making a selection
if(strDate == "") {
return;
}
// According to the docs this refers to the input
// Some digging in jquery-ujs on github make it
// look like triggering the 'submit.rails' event
// on the form will cause the normal unobtrusive
// js helpers to post the form.
// What's not clear is if the input element has the
// updated value at this point.
$(this).parent().trigger('submit.rails')
}
});
我使用的文档:
关于jquery - Rails 和 jQuery Datepicker - 选择日期时保存表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14009271/