jquery - Rails 和 jQuery Datepicker - 选择日期时保存表单

标签 jquery ruby-on-rails datepicker

当用户从日期选择器弹出窗口中选择日期(或对此进行任何更改,无论是日期还是时间)时,我想自动保存表单。在 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 UI Datepicker -- onClose

jquery-ujs on Github -- relevant lines begin at 362

关于jquery - Rails 和 jQuery Datepicker - 选择日期时保存表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14009271/

相关文章:

javascript - 如何检测我的选择表单已被ajax成功更改

javascript - 如何禁止gridster向下移动小部件元素

ruby-on-rails - 在 collection_select 上使用大写

jquery - 日期选择器的麻烦

angular - Material 2 日期选择器与日期模式验证器冲突

jQuery绑定(bind)点击事件最佳实践

javascript - 从N个这样的div中过滤时如何重新初始化一个div位置

ruby-on-rails - Chef cloudera Recipe 找不到丢失的依赖项

ruby-on-rails - Rails 4 - 政治家 - 没有方法错误

jquery - 小型 jQuery 日期选择器(CSS 问题)