我在用户的个人资料页面上有一个 jQueryUI Datepicker。当用户选择日期时,我希望立即发送 GET
请求来获取该用户在所选日期的统计信息,而不是使用日期值填充文本框。
URL 应该是这样的:
/user/1?date=20110905
然后我的 UsersController
将查看 params[date]
并查询模型以获取适当的基于日期的数据。
我遇到问题的部分是 JavaScript 部分。我需要 Hook jQueryUI Datepicker 的 onSelect
事件,但在捕获该事件之后:
- 如何从 JavaScript 代码中获取用户 ID?
- 我应该如何提交
GET
请求?我想要重新加载整页,所以我应该使用window.location
吗?
最佳答案
您可以通过location.pathname
获取URL的路径,并且可以通过将location.href
分配给您要重新加载的URL来重新加载当前页面页面与.因此,您可能会看到这样的内容:
$('#datepicker').datepicker({
onSelect: function(dateText, inst) {
// location.pathname == '/users/1'
var userId = location.pathname.split('/')[2];
var dateText = ...
location = location.href + "?date=" + dateText;;
}
});
注意,location.href
为您提供整个 URL,包括查询字符串(用于 GET 方法)和哈希,而 location.pathname
只为您提供 URL 的路径。请参阅the MDN documentation对于 location
对象。
关于javascript - 使用 jQueryUI Datepicker 根据特定日期请求 Rails View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7309792/