[英]Ruby on rails data pass to javascript become HTMLDIVELEMENT
I'm trying to pass database value retrieved by ruby to javascript,to handle some div display. 我正在尝试将ruby检索的数据库值传递给javascript,以处理一些div显示。
Strange thing is,when I pass number(means I retrieve number from database and pass to js),it works fine,as follow,this load_web is a short int. 奇怪的是,当我传递数字(意味着我从数据库中检索数字并传递给js)时,它工作正常,如下所示,此load_web是一个简短的整数。
if (<%= @device.load_web %>==0)
{ $('.somediv').find('input,textarea,select').attr('disabled',true); }
});
But when I try same method,passing string to js,it returns me HTMLDIVElement,which is very strange. 但是当我尝试相同的方法,将字符串传递给js时,它将返回我HTMLDIVElement,这很奇怪。
if(<%= @device.brand%>=="apple")
{
alert("apple");
}
else
{
alert(<%= @device.brand %>);
}
The data in my database for this colum is "apple",but the logic falls into else loop,and give me a "object HTMLDIVElement" message. 在我的数据库中,该列的数据为“ apple”,但是逻辑陷入了else循环,并给了我一个“ object HTMLDIVElement”消息。
Why is so? 为什么会这样呢?
EDIT: I try use alert(<%= @device.brand %>.innerHTML); 编辑:我尝试使用alert(<%= @ device.brand%>。innerHTML); and get the following message:
并得到以下消息:
Remember that erb tags just insert the result of the ruby code in them into your html template. 请记住,erb标签只是将其中的ruby代码结果插入到html模板中。 So unless
@device.brand
is returning some properly formatted javascript (or a number), you probably want to wrap it in strings. 因此,除非
@device.brand
返回返回格式正确的javascript(或数字),否则您可能希望将其包装在字符串中。 Try this: 尝试这个:
if("<%= @device.brand%>"=="apple")
{
alert("apple");
}
else
{
alert("<%= @device.brand %>");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.