简体   繁体   中英

How to call a JS function from rails form submit

I want to call a JS function when a button in my Rails form is clicked. The function is defined in the .js.erb file found below.

When I click the button, Chrome's JS console throws the following error:

Uncaught ReferenceError: logTime is not defined

I know this means it can't find the function, but I don't see why. Especially since I added <%= javascript_include_tag "track.js.erb" %> to the file. Any ideas?

Apologies up-front, but my Google-fu yielded no results.

_track_time_form.html.erb

<div id="countdown-timer"></div>
<div id="playback-button">&#9658;</div>
<div id="track-time-form">
    <%= form_for @project, :url => { :action => "log_time" }, remote: true do |p| %>
        <ul>
            <li><%= p.label :project, "Project:"%><br>
            <%= p.collection_select(:id, current_user.projects, :id, :name) %></li>

            <%= p.hidden_field :time_logged, :value => 0 %> <!-- value set by script in log_time.js.erb -->

            <li><%= p.submit "Log time", id: "log-time-button", :onclick => "logTime()" %></li>
        </ul>
    <% end %>
</div> 

The function the handler is calling can be found here:

track.js.erb

//initialise form 
timeTrackingForm = window.open("", "", "height=700,width=500");
$(timeTrackingForm.document.body).html("<%= j render( :partial => 'track_time_form' ) %>");

//assign variables 
var timer = $("#countdown-timer", $(timeTrackingForm.document));
var playbackControls = $("#playback-button", $(timeTrackingForm.document));
var form = $("#track-time-form", $(timeTrackingForm.document));
var formUl = $("#track-time-form ul", $(timeTrackingForm.document));
var formLi = $("#track-time-form li", $(timeTrackingForm.document));
var logTimeButton = $("#log-time-button", $(timeTrackingForm.document));
var timerPaused;

//initialise timer 
$(timeTrackingForm.document).ready(function(){
    initialiseTimer();
    style();
    $(playbackControls).click(function() {
        playOrPause();
    });
});

function initialiseTimer() {
    $(timer).timer({
        format: '%H:%M:%S'
    });
    $(timer).timer('pause');
    timerPaused = true;
}

function style() {
    $(timer).css({'color':'black','font-size':'50px', 'margin':'auto', 'width':'180px'});
    $(playbackControls).css({'color':'#290052', 'font-size':'50px', 'margin':'auto', 'width':'55px'});
    $(form).css({'width':'300px','margin':'auto'});
    $(formUl).css({'list-style-type':'none'});
    $(formLi).css({'margin':'0 0 25px 0','font-sizeL':'18px','font-family':'Arial'});
    $(logTimeButton).css({'width':'180px','font-size':'18px','background-color':'green','color':'white','margin-top':'15px'});
}

function playOrPause() {
    if (timerPaused == true) {
        $(timer).timer('resume');
        timerPaused = false;
    }
    else {
        $(timer).timer('pause')
        timerPaused = true;
    }
}

function logTime() {
    $(timer).timer('pause');
    var secondsTracked = $(timer).data('seconds');
    $('input:hidden').val(secondsTracked);
    $('#countdown-timer').timer('reset');
}

For any future readers interested in the solution, here's how I got around the issue (huge thanks to the helpful commenters above).

I added an event listener to the form in track.js.erb , which is triggered when the button is clicked (but before the form actually submits and sends data to the controller). Here it is:

var actual_form = $("#new_project", $(timeTrackingForm.document));

$(actual_form).submit(function(){ 
        $(timer).timer('pause');
        timerPaused = true;
        var secondsTracked = $(timer).data('seconds');
        $(hidden).val(secondsTracked);
        resetTimerDisplay();
    });

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