How to use variable in javascript escape_javascript rails?

主宰稳场 提交于 2021-02-08 11:56:53

问题


I need to pass a variable js to rails, as a parameter, but it is not working. Here is my code:

#function js
function load_custom_fields(){
  var id = $("MY_SELECT").val();
  $(".productHere").html("<%= escape_javascript(render :partial =>'my_partial', :locals => { id:id} ) %>");

}

回答1:


From

"<%= escape_javascript(render :partial =>'my_partial', :locals => { id:id} ) %>"

To

"<%= escape_javascript(render :partial =>'my_partial', :locals => { id:"+id+"} ) %>"



回答2:


A simple solution is to add a placeholder inside the escape_javascript and then replace it with your JavaScript variable, for example:

var id = $("MY_SELECT").val();
$(".productHere").html("<%= escape_javascript(render :partial =>'my_partial', :locals => { id: '_ID_'} ) %>".replace("_ID_", id);


来源:https://stackoverflow.com/questions/38794605/how-to-use-variable-in-javascript-escape-javascript-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!