0

I have a very simple comment form, which submits through $.ajax. However, I'm having some problems with it refreshing on submit.

This happens even though I am using both e.preventDefault() and return false within my submission script.

I have looked at every single post I can find on StackOverflow and even various Fiddles, but have come across no solutions.

This is my JavaScript:

$(".f-track-actions__form").submit(function(ev){
    ev.preventDefault();

    var wave = $.cookie("cv"),
        wave = eval(wave),
        waveDuration = wave.getDuration();

    var child = $(this).children(".f-track-actions__form-comment");
    var cTime = (wave.getCurrentTime() / waveDuration) * 100;
    var pD = {
        'c': comment.val(),
        'i': child.attr('id'),
        't': cTime
    };

    $.ajax({
        type: "post",
        url: "/spectrum-rr/core/_func/functions/actionTrack.php",
        data: pD,
        success: function(data) {
            alert(data);
        }
    });

    return false;
});

HTML:

<form method="post" class="f-track-actions__form">
    <input class="f-track-actions__form-comment" type="text" placeholder="Comment on this track..." name="comment" id="c-1">
</form>

Thanks.

GROVER.
  • 4,071
  • 2
  • 19
  • 66

3 Answers3

0

I have used this function before and it is working with e.preventDefaults

$('#contactForm1').on('submit',function(e){
    e.preventDefaults();
});
GROVER.
  • 4,071
  • 2
  • 19
  • 66
Assem Mahrous
  • 411
  • 3
  • 15
0

Change your html to this,

<form action="" method="post" class="f-track-actions__form">
    <input class="f-track-actions__form-comment" type="text" placeholder="Comment on this track..." name="comment" id="c-1">
</form>

The submit handler is not working, because you missed to add action attribute to the form

akhil.cs
  • 691
  • 1
  • 5
  • 12
0

Make sure you're binding your button in the document ready event...

$( document ).ready() {
    $(".f-track-actions__form").submit(function(ev){
        ...
        return false;
    }
}
bastos.sergio
  • 6,684
  • 4
  • 26
  • 36
  • There's no available button. The user must hit enter to submit. And, they're already within the `$(document).ready()` event – GROVER. Nov 24 '16 at 10:35
  • You haven't shown your code wrapped up in the document ready event. Regardless, if you're generating the form dynamically there's a chance that the document ready event runs too late and is not binding the form. Also if there's multiple instances of the form, then there's a change that one of the other forms is not binding correctly and is somehow triggering a postback. Either way, without a way to replicate the error on a jsfiddle, I'm afraid I can't really help... – bastos.sergio Nov 24 '16 at 10:49
  • ended up fixing it :) – GROVER. Nov 24 '16 at 11:10
  • What's the solution? If you don't post the answer, then this question won't really help others with the same problem. – bastos.sergio Nov 24 '16 at 11:24