我有一个用于编辑配置文件的表单。 Rails 自动将表单 ID 生成为“edit_profile_##”,其中 ## 是当前用户的配置文件 ID(实例变量-@profile_id)。我需要将此表单 ID 用于我的 javascript 函数。有没有办法在js中获取当前用户的个人资料ID?或者有什么方法可以覆盖 Rails 自动生成的 ID?
最佳答案
你必须使用函数参数发送它
.html.erb
<script type="text/javascript">
var user_id = <%= @profile_id %>; // for integer
var user_name = '<%= @profile_name %>'; // for string
abc(user_id)// this is your function in .js file
</script>
.js
function abc(id){
alert(""+id)
}
关于javascript - 如何在 Ruby on Rails 应用程序中访问 Javascript 函数内的实例变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3155090/