如何在 Ruby 注入中插入 Javascript 代码
我有 2 个文本字段。一个是会员id的输入,一个是会员姓名的输出。
当我在第一个字段中输入成员的 ID 并触发 onblur
操作时,我想在另一个字段中显示该成员的姓名。
这是我的 Javascript 函数:
function show_name(){
id = document.getElementById('id_member').value;
field_name = document.getElementById("name_member");
field_name.value = "<%= Member.find().nom %>";
}
在方法 Member.find()
中,我想发送我在开头声明的变量 id
的值。示例:
field_name.value = "<%= Member.find(*Javascript "ID" variable value*).nom %>";
如何将该值插入到 Ruby 注入中?
谢谢。
I have 2 text fields. One is for the input of a member's id, and the other is the output of the member's name.
When I type a member's id in the first field and the action onblur
is triggered, I would like to show the member's name in the other field.
This is my Javascript function:
function show_name(){
id = document.getElementById('id_member').value;
field_name = document.getElementById("name_member");
field_name.value = "<%= Member.find().nom %>";
}
In the method Member.find()
I would like to send the value of the variable id
that I've declared at the beginning. Example:
field_name.value = "<%= Member.find(*Javascript "ID" variable value*).nom %>";
How can I insert that value into my Ruby injection?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Javascript在客户端运行,ruby在服务器运行。除非您向服务器发出新请求,否则您无法将 javascript 变量传递到 ruby 代码中。
您的选择是使用新查询重新加载整个页面,使用 ajax 进行新查询并对结果执行某些操作,或者提前将所有可能的成员下载到页面中并让 javascript 以某种方式将其从页面中拉出。
Javascript is run on the client, ruby is run on the server. You can't pass a javascript variable into the ruby code, unless you make a new request to the server.
Your options are to reload the whole page with a new query, use ajax to make a new query and do something with the result, or to download all possible Members into the page ahead of time and have javascript pull it out of the page somehow.