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

前端 未结 8 1952
时光取名叫无心
时光取名叫无心 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:51

    Let's make shure we understand each other. Your erb template (new_daily.js.erb) will be processed on the server side, ruby code will be evaluated (within <% %>), substitution made, and then resulting javascript will be sent to browser. On the client side the browser will then evaluate this javascript code and variable i will be assigned a value.
    Now when do you want to pass this variable and to what partial?

    0 讨论(0)
  • 2020-11-27 06:56

    Yes you can pass the value by using jquery;

    <%=f.text_field :email ,:id=>"email_field" %>
    
    <script type="text/javascript">
       var my_email= "my@email.com"
       $(document).ready(function(){
            $("#email_field").val(my_email);
       });
    </script>
    
    0 讨论(0)
提交回复
热议问题