简体   繁体   English

rails + escape_javascript +语言环境

[英]rails + escape_javascript + locales

I have the below jquery script and I can't pass i value into locales=> :val 我有下面的jQuery脚本,我不能将我的值传递给locales =>:val

$(document).ready(function(){

  var i = 1

  function more(){

     var information= $("<%= escape_javascript(render :partial => 'info', :locals => { :val => i }) %>");

     i = i+1;
  }

});

Please guide me how to pass javascript variable to rails. 请指导我如何将JavaScript变量传递给Rails。

Thanks 谢谢

If possible values of the variable i are limited, you could generate javascript for every possible value of ieg: (i is in range 1..10) 如果变量i的可能值受到限制,则可以为ieg的每个可能值生成javascript:(i范围为1..10)

$(document).ready(function(){
  var i = 1
  var informationArray = new Array();
  <% (1..10).each do |num| %>
    informationArray[<%= num %>] = $("<%= escape_javascript(render :partial => 'info', :locals => { :val => num }) %>");
  <% end %>

  function more(){
    var information = informationArray[i];
    i = i+1; 
  }

});

Otherwise, if variable i is not limited in a specific small range, you should use AJAX to send the parameter to the server and retrieve the information for that value. 否则,如果变量i不限于特定的小范围,则应使用AJAX将参数发送到服务器,并检索该值的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM