Change Jquery is being triggered without change

Asked

Viewed 53 times

0

Hello,

I have a JS code that gives the value for a price slider, and in this JS has a change that is being triggered and inside the change has a Rigger that triggers another part of the JS that does the post and redirects the page but the change is being triggered without any change and the page is reloading all Ra, code:

/*slider jQuery UI*/
(function () {   
    var pdtPrice = $('form[name="workcontrol_filter"]').find('input[name="pdt_price"]');
    var pdtMinPrice = parseInt(pdtPrice.attr('data-min'));
    var pdtMaxPrice = parseInt(pdtPrice.attr('data-max'));

    var pdtStepPrice = parseInt(pdtPrice.attr('data-step'));
    var pdtRangePrice = parseInt(pdtPrice.attr('data-range'));

    $("#slider_price").slider({
        range: true,
        min: pdtMinPrice,
        max: pdtMaxPrice,
        values: [pdtStepPrice, pdtRangePrice],
        slide: function (event, ui) {
            $("#amount").val("R$ " + ui.values[0] + " - R$ " + ui.values[1]);
        },
        // Esse change está sendo acionado sem ter alteração nenhuma no slider_price
        change: function (event, ui) {
            pdtPrice.val(ui.values[0] + ',' + ui.values[1]);
            //alert('FOI ACIONADO');
            $('form[name="workcontrol_filter"]').trigger('change');
        }
    });

    if (pdtMinPrice === pdtMaxPrice) {
        $("#slider_price").slider("option", "disabled", true);
    }

    $("#amount").val("R$ " + $("#slider_price").slider("values", 0) + " - R$ " + $("#slider_price").slider("values", 1));
})();

HTML:

<div id="slider_price"></div>

Code that Trigger triggers:

/*change form*/
$('html').on('change', 'form[name="workcontrol_filter"]', function () {
    $.post(action, $(this).serialize(), function (data) {
        $('html, body').animate({scrollTop: 0}, 200, function () {
            window.location.href = BASE + '/' + data.redirect;
        });
    }, 'json');
});

Then someone knows why it happens?
And I don’t know if it matters, but I use Twig for html.

  • Hello Wiliam, you can create an example that runs, in jsFiddle or in the tool here on the site?

  • I’m no expert on jquery but, you onchange is inside a document.ready ?? I think it is a good attempt, if not so, use, as ensures that only the event will be triggered, after loading the whole page, always.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.