How to pass a javascript variable into a erb code in a js view?

前端 未结 8 2096
时光取名叫无心
时光取名叫无心 2020-11-27 06:19

I have this Javascript view in my Rails 3 project:

app/views/expenses/new_daily.js.erb

var i = parseInt($(\'#daily\').attr(\'data-num\')) + 1;
//$(\'         


        
8条回答
  •  北荒
    北荒 (楼主)
    2020-11-27 06:36

    As far as i know there is no way to do it directly and the reason is fairly simple too, html is executed at the server side and javascript is a client side language which means its executed in your local browser, thats why if you even try to pass a variable between the two you'll have to make a request to the server, However this problem is tackled by calling an AJAX request, this AJAX request does the same thing as sending a new request to the server however it does that without refreshing or reloading the page to it gives the users the illusion that no request was made.

    a guy asks a similar question Here

    and you can learn more about AJAX Here on MDN:

提交回复
热议问题