ajax with select tag rails 4
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);
});
});
更多相关文章
- 使用jquery tablesorter插件,有一个选中项目的下拉框
- jquery不会对select/选项更改事件作出反应。
- jQuery 三级联动选项栏
- 使用Next设置选定的选项
- jquery.min.js引入项目报错
- 基于jQuery实现页面搜索功能----项目实战
- 当您有很多选项时,GUI替代
- 如何使用meta在“NEW”选项卡中重定向
- 开源项目Html Agility Pack实现快速解析Html