简体   繁体   中英

Triggering change and load events on ajax loaded content

I have a code which runs on change event of select box #type as id. The element is loaded via ajax in bootstrap modal window. It works fine on change event by showing/hiding relevant elements and firing an ajax request.

Now I want to run it everytime, ajax content is loaded as well, apart from change event, because sometimes a default value is loaded for #type element and I want to show/hide elements and fire ajax request on ajax content load that time.

I tried to use load event like this,

$(document).on('load change', '#geo-form #type', function () {...

but id didn't work. How can I make this work.

Here is the default working code, as of now.

$(document).on('change', '#geo-form #type', function () {
    var value = $(this).val();
    console.log(value);
    if (value == '') {
        $.getJSON(url('ajax/divisions'), null, function (data) {
            $("#geo-form #division_id").html('');
            $("#geo-form #division_id").append(
                $("<option></option>").text('Select one').val('')
            );
            $.each(data, function (id, name) {
                $("#geo-form #division_id").append(
                    $("<option></option>").text(name).val(id)
                );
            });
        });
    }
    switch (value) {
        case 'district':
            $('#geo-form .division').removeClass('hide');
            $('#geo-form .district').addClass('hide');
            $('#geo-form .tehsil').addClass('hide');
            $('#geo-form .block').addClass('hide');
            break;
        case 'tehsil':
            $('#geo-form .division').removeClass('hide');
            $('#geo-form .district').removeClass('hide');
            $('#geo-form .tehsil').addClass('hide');
            $('#geo-form .block').addClass('hide');
            break;
        case 'block':
            $('#geo-form .division').removeClass('hide');
            $('#geo-form .district').removeClass('hide');
            $('#geo-form .tehsil').removeClass('hide');
            $('#geo-form .block').addClass('hide');
            break;
        case 'village':
            $('#geo-form .division').removeClass('hide');
            $('#geo-form .district').removeClass('hide');
            $('#geo-form .tehsil').removeClass('hide');
            $('#geo-form .block').removeClass('hide');
            break;
    }
});

您可以在重新创建时手动触发它:

$("#geo-form #division_id").trigger('change');

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