【问题标题】:coffeescript .on "change" duplicate ajax callscoffeescript .on“改变”重复的ajax调用
【发布时间】:2017-09-15 14:05:16
【问题描述】:

我已经使用 .on "change" 事件来更新名为 "form_name" 的文本字段中的记录

此“form_name”文本字段的行为类似于搜索框以及下拉菜单,请参见下图

text field image

当我输入 form_name 时,它​​会搜索表单名称,然后我选择表单记录的名称被保存两次。

咖啡脚本->

$(document).on "change", ".form-requirement-name", (e) ->
self = $(this)
id = self.data("id")
value = self.val()
request = $.post("/programs/#{id}/form_requirement_name", {"form_name": value})
request.done (data, textStatus, jqXHR) ->
  toastr.success('Form action updated.')

form_requirement_name.controller.rb

def form_requirement_name
form_name = params[:form_name]
if is_temp?(params[:id])
  session[:temp_form_list_rule][:form_id] = form_name
  save_temp_form_list_rule
  respond_to do |format|
    if @form_list_rule.form_id.present?
      format.json {render json: @form_list_rule}
    else
      format.json {head :ok}
    end
  end
end

结束

请帮帮我。

【问题讨论】:

  • 你能检查一下你是在一个选择中得到两个 ajax 调用还是一个 ajax 调用但有两个保存操作?
  • @EJ2015 我收到两个 ajax 调用

标签: javascript jquery ruby-on-rails ajax coffeescript


【解决方案1】:

尝试在 ajax 调用之前添加以下内容:

e.preventDefault();
e.stopPropagation();

【讨论】:

    【解决方案2】:

    这是因为您应该绑定onkeyuponkeydown 事件而不是change

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-04
      • 2012-03-28
      • 1970-01-01
      相关资源
      最近更新 更多