I am using rails 4 to make an app like craigslist. I have a list of items on my index page. I am trying to create a dropdown select with options 'price', 'Most recent' so that users can sort the items based on these. Now, i need to send an ajax request to my '/items' url with params 'price' or 'most recent'. I have tried a lot of things but none have worked so far and i am completely stuck. This is the latest code i have. Are there any UBJ parameters i can use to accomplish this? I am new to rails and especially ajax, any help would be appreciated? Thanks in advance.

我使用rails 4制作像craigslist这样的应用程序。我的索引页面上有一个项目列表。我正在尝试使用选项“价格”,“最新”创建下拉列表选项,以便用户可以根据这些项目对项目进行排序。现在,我需要向我的'/ items'网址发送一个ajax请求,其中包含params'price'或'most recent'。我已经尝试了很多东西,但到目前为止还没有工作,我完全陷入困境。这是我的最新代码。我可以使用任何UBJ参数来完成此任务吗?我是rails的新手,尤其是ajax,任何帮助都会受到赞赏吗?提前致谢。

<script>
     $('#sort_by').on('change',function()
     {
         $.ajax({ type: "GET",
           url: "/items",
           data: {sort: $(this).val();},
           return false
         })
     }
</script>

2 个解决方案

#1


1

Try this

$('#sort_by').on('change', function(){
  $.ajax({ type: "GET",
     url: "/items",
     data: { sort: $('option:selected', this).val() }
  }).done(function(data) {
    console.log(data);
  });
});

更多相关文章

  1. 使用jquery tablesorter插件,有一个选中项目的下拉框
  2. jquery不会对select/选项更改事件作出反应。
  3. jQuery 三级联动选项栏
  4. 使用Next设置选定的选项
  5. jquery.min.js引入项目报错
  6. 基于jQuery实现页面搜索功能----项目实战
  7. 当您有很多选项时,GUI替代
  8. 如何使用meta在“NEW”选项卡中重定向
  9. 开源项目Html Agility Pack实现快速解析Html

随机推荐

  1. Couldn't find meta-data for provider w
  2. .net程序员转战android第二篇---牛刀小试
  3. 理解Android的菜单
  4. Android之MPAndroidChart库使用说明(柱状
  5. Android NFS文件系统挂载遇到的问题解决
  6. Android里面常用的Drawable的使用
  7. android读取keystore证书文件
  8. Android:Field can be converted to a lo
  9. Android TextView显示文字过长时添加阴影
  10. 基于原生 EditText+span 实现的 Android