weixin_33725807 2015-10-12 21:53 采纳率: 0%
浏览 14

Rails Ajax动态选择

Seems that the code is correct!

so, someone could spare any information why the second(subcategories) partial did not update? Thank's

I put on js:

$(document).ready(function()  {
  return $(document).on('click', "input[type='radio'][name='product[gender_id]']", function(evt) {
    return $.ajax('update_category_select', {
      type: 'GET',
      dataType: 'script',
      data: {
        gender_id: $("input[type='radio'][name='product[gender_id]']:checked").val()
      },
      error: function(jqXHR, textStatus, errorThrown) {
        return console.log("AJAX Error: " + textStatus);
      },
      success: function(data, textStatus, jqXHR) {
        return console.log("Dynamic state select OK!");
      }
    });
  });

  return $(document).on('change', '#categories_select', function(evt) {
    return $.ajax('update_subcategory_select', {
      type: 'GET',
      dataType: 'script',
      data: {
        category_id: $("#categories_select option:selected").val()
      },
      error: function(jqXHR, textStatus, errorThrown) {
        return console.log("AJAX Error: " + textStatus);
      },
      success: function(data, textStatus, jqXHR) {
        return console.log("Dynamic state select OK!");
      }
    });

  });
});

First Partial:

$("#categories_select").empty().append("<%= escape_javascript(render(:partial => @categories)) %>")

If the firt partial is updated update the second partial

Second partial:

$("#subcategories_select").empty().append("<%= escape_javascript(render(:partial => @subcategories)) %>")

Products Controller

 @catagories = Category.where("gender_id = ?", params[:gender_id])
    respond_to do |format|
      format.js
    end
  end


 def update_subcategory_select


    @subcategories = Subcategory.where("category_id = ?", params[:category_id])
    respond_to do |format|
      format.js
    end
  end
  • 写回答

1条回答 默认 最新

  • weixin_33694620 2015-10-13 20:52
    关注

    The return is used inside a function to return a value/expression and the following sentences are not executed.

    In your javascript do you have something like:

    function(){ //Document ready
      return 1; //Here you on click
      return 2; //Here you on change
    }
    

    So return 2 is never called. In your case, the event handler for 'change' is never attached so subcategories are not updated.

    Just remove all return in your javascript code.

    评论

报告相同问题?

悬赏问题

  • ¥15 三菱变频器模块怎么检测触发
  • ¥15 在C#方法中使用了.Refresh();刷新了控件,但是不是最新值
  • ¥30 求一下解题思路,完全不懂。
  • ¥30 关于#硬件工程#的问题:求一下解题思路
  • ¥15 运筹学对偶单纯行法构造扩充问题
  • ¥20 XP系统的老电脑一开机就提示找不到rundll.exe,付费求解
  • ¥15 milvus查询出来的score怎么转换成0-1之间的相似性
  • ¥15 lvgl V9移植到linux开发板
  • ¥15 VB.net中在窗体中创建一个button控件来关闭窗体,但是提示错误,我该怎么办
  • ¥15 网上下载好的程序但是arduinoIDE编程报错,运行不了,哪里出错了,能具体给改一下吗