tuupola / jquery_chained

Chained Selects for jQuery and Zepto
https://appelsiini.net/projects/chained/
589 stars 283 forks source link

Prevent from firing ajax call on second select #48

Closed MobsterGit closed 8 years ago

MobsterGit commented 8 years ago

This is not an issue per say, but I'm using ajax .on('change') to load products after the second select is populated.
For some reason, when the second select #products populates, it's also firing my .on('change') function.
Is there a way to prevent this from happening in the .remoteChained function?

This:

` $(function() { // More code using $ as alias to jQuery /* For jquery.chained.remote.js */ $("#products").remoteChained({

            parents: "#instrument",
            url: "http://sitstringsdesign.com/ajax/instrument",
            loading: "loading",
      addclass: ""

        });

});`

Also fires this:

`$('#products').on('change', function(event) { event.preventDefault(); var href = $(this).find('option:selected').attr('value'); console.log('href ' + href); location.hash = href; $('#sort').hide(); $('#sort').css({ display: 'block' }).animate({ height: '100%' }, function() {

    $('#sort').fadeIn('slow').html('<div class="clearfix loading"> LOADING </div>');
    $('#loader').css({ border: 'none', position: 'relative', top: '24px', left: '48px', boxShadow: 'none'
    }); 
 // http://loadinfo.net/
    $.ajax({
        url: "/ajax/ajax_sort/" + href,
        dataType: "html",
        success: function(data) {
            $("#sort").html(data);
      $('.c3 .card').equalHeights();
            $('.product-count').fadeOut('slow');
            $('#ajax-hidden').hide().fadeOut('slow');
        }
    });
});

` Thank you!

tuupola commented 8 years ago

The change event is firing on #products because the contents of #products changes. Thus this is expected behaviour.

You could check what is the value of #products to see if you actually need to fire the request to /ajax/ajax_sort/.

MobsterGit commented 8 years ago

Thanks Tuupola,

I actually resolved the issue by changing .trigger to .on in the remote js file. Everything still loads, but allows me to have the option to activate the Ajax call after its populated.

Thanks much!