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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵