简体   繁体   English

将Javascript变量传递给Rails form_for hidden_​​field

[英]Passing a Javascript Variable to a Rails form_for hidden_field

I have borrowed a fairly complex JS stopwatch and am trying to report the resulting time to my Rails app's database. 借用了一个相当复杂的JS秒表,并试图将结果时间报告到我的Rails应用程序的数据库中。 I have consulted many similar SO posts , but haven't been able to get it working. 我已经咨询了许多类似的SO职位 ,但一直无法使其正常工作。

Here is my ERB: 这是我的ERB:

<%= form_for @new_log do |f| %>
...
<div class="row text-center">
   <div class="btn-white" id="startTime" style="display: inline-block;">
        <h4 value="start" onclick="start();">Start Timer</h4>
    </div>
    <div style="display: inline-block;">
        <span id="time" style="color: white"></span><br>
        <span class="btn-white" value="reset" onclick="reset()">Reset</span>
    </div>
    <div class="btn-white" id="stopTime" style="display: inline-block;">
        <h4 value="stop" onclick="stop();">Stop Timer</h4>
    </div>
    <%= f.hidden_field :duration, id: "duration" %>

         ...

    <%= f.submit "Record My Log", id: "submit", class: "btn-ghost" %>

<% end %>

Here's the JS: 这是JS:

<script> // STOPWATCH
    var clsStopwatch = function() {
        // Private vars
        var startAt = 0;    // Time of last start / resume. (0 if not running)
        var lapTime = 0;    // Time on the clock when last stopped in milliseconds

        var now = function() {
                return (new Date()).getTime(); 
            }; 

        // Public methods
        // Start or resume
        this.start = function() {
                startAt = startAt ? startAt : now();
            };

        // Stop or pause
        this.stop = function() {
                // If running, update elapsed time otherwise keep it
                lapTime = startAt ? lapTime + now() - startAt : lapTime;
                startAt = 0; // Paused
            };

        // Reset
        this.reset = function() {
                lapTime = startAt = 0;
            };

        // Duration
        this.time = function() {
                return lapTime + (startAt ? now() - startAt : 0); 
            };
    };

    var x = new clsStopwatch();
    var $time;
    var clocktimer;

    function pad(num, size) {
        var s = "0000" + num;
        return s.substr(s.length - size);
    }

    function formatTime(time) {
        var h = m = s = ms = 0;
        var newTime = '';

        h = Math.floor( time / (60 * 60 * 1000) );
        time = time % (60 * 60 * 1000);
        m = Math.floor( time / (60 * 1000) );
        time = time % (60 * 1000);
        s = Math.floor( time / 1000 );
        ms = time % 1000;

    newTime = pad(h, 2) + ':' + pad(m, 2) + ':' + pad(s, 2) + ':' + pad(ms, 3);
    return newTime;

    // THIS IS THE PART I ADDED, WHICH IS NOT WORKING
    $('#submit').on('click', function() {
      $('#duration').val(newTime);
    });
    // MY ADDITION ENDS HERE
}

function show() {
    $time = document.getElementById('time');
    update();
}

function update() {
    $time.innerHTML = formatTime(x.time());
}

function start() {
    clocktimer = setInterval("update()", 1);
    x.start();
}

function stop() {
    x.stop();
    clearInterval(clocktimer);
}

function reset() {
    stop();
    x.reset();
    update();
}
</script>

Hopefully this will be a fairly simple fix for someone with a better eye for JS than mine. 希望对于那些比我更了解JS的人来说,这将是一个相当简单的解决方案。 Currently the stopwatch and the form submission still work, but the :duration still gets recorded into the database as nil . 目前,秒表和表单提交仍然有效,但是:duration仍以nil记录到数据库中。

Thanks for any help you can spare! 多谢您的协助!

Your event method never gets called because the formatTime function returns newTime before it can reach it. 您的事件方法永远不会被调用,因为formatTime函数newTime在到达它之前返回newTime Try moving the click function out of the formatTime function. 尝试将click函数移出formatTime函数。 Remember to set the return value of formatTime to a variable because newTime will be out of scope. 请记住将formatTime的返回值设置为变量,因为newTime将超出范围。

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

相关问题 一个表单中的多个hidden_​​field? - Multiple hidden_field in one form? 如何在 Javascript 中为 hidden_​​field 设置整数? - How to set a integer for a hidden_field in Javascript? Rails hidden_​​field或JavaScript数组被发布为逗号分隔的字符串,而不是数组 - Rails hidden_field or JavaScript array is being posted as comma separated string instead of array 如何将值从js传递到Rails中的hidden_​​field? - How to pass value from js to hidden_field in Rails? 单击Rails form_for选择字段后激活Javascript函数 - Activate Javascript function after clicking on Rails form_for select field Rails 使用 javascript 将值传递给隐藏字段 - Rails passing value to hidden field using javascript 将JavaScript变量传递给codeigniter中的隐藏形式 - passing a javascript variable into a hidden form in codeigniter Rails 4:将局部变量从包含form_for帮助器的部分传递给js.erb文件 - Rails 4: Passing a local variable to a js.erb file from a partial containing a form_for helper 如何在不丢失正确格式的情况下使用Javascript更改hidden_​​field数组? - How to change a hidden_field array using Javascript without losing its correct format? 使用 Javascript 优雅地将字段值传递给变量和隐藏输入 - Passing field values into variable and hidden input with Javascript elegantly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM