如何在rails上的ruby中使用javascript获取datetime

编程入门 行业动态 更新时间:2024-10-27 20:32:57
本文介绍了如何在rails上的ruby中使用javascript获取datetime_select字段的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个以前的问题触动了这个话题( Rails:在textfield中执行onchange远程函数时如何从另一个字段获取值),但是我稍微深入一点,意识到我的问题是datetime_select方法。 / p>

我有一个带onchange的文本框。在onchange中,当我将ajax调用到我的控制器时,我需要获取datetime_select字段的值作为参数传递。首先要注意的是,datetime_select实际上由五个下拉列表(年,月,日,时,分)组成,所以我需要分别获取每个下拉列表的值(我猜想,是否有更简单的方法?) 。但是,当我尝试通过以下代码获取他们的价值:

$(model +'_'+ attribute +'_3i')应该返回day下拉列表的值,我得到一个错误:TypeError:$$ $ $ $ $ $ $ $ $ $ $ $ $ $ $(model_attribute_3i)。value.toS不是函数

如果我手动创建datetime对象的所有下拉列表,同样的调用工作正常。所以似乎有一些魔术,datetime_select的组合方式阻止我询问下拉列表的值。有没有办法解决这个问题,或者我只需要在我的应用程序中重新编码datetime_select功能?

解决方案

现在解决,问题是我是一个javascript n00b ...显然我在太多的地方使用了相同的变量名,导致了奇怪的行为。

I had a previous question that touched this topic (Rails: how to get value from another field when executing an onchange remote function in textfield), but then I dugg a little deeper and realized that my problem is the datetime_select method.

I have a textfield with onchange. In the onchange I need to get the value of a datetime_select field to pass as a parameter when making the ajax call to my controller. The first thing to note is that a datetime_select is actually composed of five dropdowns (year, month, day, hour and minute), so I need to get the values of each of them separately (I presume, is there a simpler way?). But when I try to get their value with code like:

$(model + '_' + attribute + '_3i').value

which should return the value of the "day" dropdown I get an error: "TypeError: $("model_attribute_3i").value.toS is not a function"

If I create all the dropdowns for the datetime objects manually the same call works just fine. So there seems to be some magic in the way a datetime_select is composed that prevents me from asking the value of the dropdowns. Is there any way to go around this or do I just have to recode the datetime_select functionality in my application?

解决方案

Problem seems to be solved for now, problem was me being a javascript n00b... apparently I used same variable names in too many places which leaded to weird behavior.

更多推荐

如何在rails上的ruby中使用javascript获取datetime

本文发布于:2023-10-17 22:19:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1502240.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   rails   ruby   javascript   datetime

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!