简体   繁体   English

在ajax加载的内容上触发更改和加载事件

[英]Triggering change and load events on ajax loaded content

I have a code which runs on change event of select box #type as id. 我有一个代码,它在选择框#type作为id的change事件上运行。 The element is loaded via ajax in bootstrap modal window. 该元素是通过Ajax在引导模式窗口中加载的。 It works fine on change event by showing/hiding relevant elements and firing an ajax request. 通过显示/隐藏相关元素并触发ajax请求,它可以很好地处理更改事件。

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. 现在,我想每次都运行它,除了change事件之外,Ajax内容也要加载,因为有时#type元素会加载默认值,并且我想显示/隐藏元素并在加载Ajax内容时触发Ajax请求。

I tried to use load event like this, 我试图使用这样的load事件,

$(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');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM