将ruby变量传递给javascript(Ruby on Rails) [英] passing ruby variables to javascript (Ruby on Rails)
问题描述
我有一份药物清单和一份疫苗清单,我分别从药品和疫苗中提取。药物清单的格式为medicine_name,疫苗清单也是如此。我使用这段代码将这两个存储在变量中。
I have a list of medicines and a list of vaccinations that I am extracting from tables medicines and vaccinations respectively. The list of medicines is of the format medicine_name and so is the list of vaccinations. I store both of these in vars using this piece of code.
var injections=<%=@injections%>;
var medicines=<%=@medicines%>;
在我看来,我选择药物和注射标签但是当我尝试将这些变量附加到使用以下代码的相应标签。我没有得到任何结果请帮助
In my view I have select tags for medicines and injections but when I try to append these vars to the respective tags using the following code. I don't get any results please help
$('.addmedicines').click(function()
$('.medicines').append(medicines);
});
$('.addinjections').click(function(){
$('.injections').append(injections);
});
这里.injections是指定用于接种疫苗标签的类。 .medicines是指定用于选择药物标签的类。而.addmedicines和.addinjections是html按钮。
Here .injections is the class assigned to select tag for vaccinations. .medicines is the class assigned to select tag for medicines. And .addmedicines and .addinjections are html buttons.
编辑:
当我尝试同时显示两次注射时javascript中的药品如下所示:
When I try to display both injections and medicines in javascript they display like this:
药品:
<option>Med1</option><option>Med2</option><option>Med3</option><option>Med4</option>
注射:
<option>i1</option><option>i2</option><option>i3</option><option>i4</option>
推荐答案
var injections= '<%=@injections%>';
var medicines='<%=@medicines%>';
这篇关于将ruby变量传递给javascript(Ruby on Rails)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!