简体   繁体   中英

Pass value of JavaScript variable to Rails view with hidden_field_tag helper

I set up a JavaScript program using jQuery to change the user input values at various form field.

$(document).ready(function(){
    $("#billAmt").keyup(function(){
        var a = $("#billAmt").val();
        var b = a*3/100;
        var d = "<%= current_user.balance %>";
        var c = d - b;
        $("#cashBack").val(b);
        $("#total").val(c);
    });
});

This JavaScript code is inside the form_tag and I would like to pass #cashBack to hidden field.

I did like:

<%= hidden_field_tag :cashBack, nil, :id => "cashBack", :value => ''%>

When I submit the form, params[:cashBack] is empty. So, how do I pass the value and change every time I change the value.

为了设置隐藏的文本字段,您必须使用纯 javascript 而不是 JQuery。

$("#cashBack").get(0).value = b;                  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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