【问题标题】:javascript function not working in jquery-ajax rails validation error displaysjavascript 函数在 jquery-ajax rails 验证错误显示中不起作用
【发布时间】:2013-07-08 08:36:35
【问题描述】:

我有一个全局的 Jquery UI Datepicker 函数来在所有页面中使用日历。 我创建了一个单独的 javascript 页面,如下所示:

var showDatePickers = function() {
  $('[data-field-type="date"]').datepicker({
    dateFormat: "yy-mm--dd",
    showOn: "both",
    buttonImageOnly: true,
    buttonImage: "/assets/blue-calendar-icon.png",
    buttonText: "Calendar"
  });
}

$(showDatePickers);

我只是在我的视图中发布我的日期选择器字段,

<div class="field">
  <%= f.label :Renewal_Date %>
  <%= f.text_field :Renewal_Date, readonly: 'readonly', data: {field_type: date}}
</div>

我将上述函数调用到一个单独的 javascript 文件中。

$(function() {
  if ($('html.asset_contracts').length == 1) {
    $(document.body).on('ajax:success', '.new_asset_contract, .edit_asset_contract',     showDatePickers);
  }
});

页面加载、编辑和新操作时工作正常。但是当 rails 验证错误显示 datepicker 函数不起作用时。它显示空白text_field

仅供参考:这是一个 ajax 页面,new, create, update and edit 操作作为ajax 页面工作。所以,我在我的表单中添加了remote: true,我有new.js, edit.js, create.js and update.js

这是我的控制器,

def create
     @contract = Asset::Contract.new(params[:asset_contract])

     respond_to do |format|
       if @contract.save
         format.html { redirect_to asset_contracts_path, notice: "Successfully Created" }
         format.js
         format.json { render json: @contract, status: :created, location: @contract }
       else
         format.html { render action: "new" }
         format.js
         format.json { render json: @contract.errors, status: :unprocessable_entity }
       end
     end
   end



   def update
     @contract = Asset::Contract.find(params[:id])

     respond_to do |format|
       if @contract.update_attributes(params[:asset_contract])
         format.html { redirect_to asset_contracts_path, notice: "Succesfully Updated" }
         format.js
         format.json { head :no_content }
       else
         format.html { render action: "edit" }
         format.js
         format.json { render json: @contract.errors, status: :unprocessable_entity }
       end             
     end
   end

谢谢

【问题讨论】:

  • 发生验证错误时,检查js是否修改日期时间字段选择器[data-field-type="date"]

标签: ruby-on-rails ruby-on-rails-3 jquery-ui jquery


【解决方案1】:

您正在像这样创建日期选择器:

$(document.body).on('ajax:success', '.new_asset_contract, .edit_asset_contract',     showDatePickers);

但是,这只会在 AJAX 调用成功时运行,因此您还需要一个错误处理程序。

看来您正在使用命名空间事件,而我在 jQuery 文档中没有看到该事件的引用。您可能希望使用全局 ajax 事件(例如,ajaxComplete、ajaxError 等)。

您需要为ajaxError 附加一个单独的处理程序来处理错误情况,或者只使用ajaxComplete 事件代替ajax:success。除非您需要特定的错误处理,否则ajaxComplete 是可行的方法,因为您只需要编写/维护一个处理程序。从 jQuery 1.8 开始,全局事件在 document 上触发。您需要将侦听器附加到文档中,而不需要任何其他选择器:

$(document).on('ajaxComplete', showDatePickers);

您可以在Ajax Events 页面上阅读有关 jQuery AJAX 事件的更多信息。

【讨论】:

  • 我试过ajax:complete, ajax:error, ajax:success 没有运气。谢谢。
  • 我更新为使用全局事件而不是命名空间事件。试试看它是否有效。
  • ajax:complete 不是工作伙伴。可能我需要尝试为ajaxError 附加一个单独的处理程序。请指导我这样做。
  • 你试过ajaxComplete吗?这与ajax:complete 不同。
  • 我还更新了处理程序的注册方式。看看这些更新是否对您有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多