Chosen ajax on ready

本秂侑毒 提交于 2019-12-25 04:43:25

问题


I try to use jquery chosen with ajax call : https://harvesthq.github.io/chosen/

My html is :

<div class="form-group m-b logiciel">
    <label>Logiciel concerné</label>
    <select id="logiciel" name="logiciel" class="chosen">
    </select>
</div>

My first script is :

$(document).ready(function () {
    $("#logiciel").load('ajax/ticket_add_select.ajax.php?id=' + $('#client').val());
    $("#logiciel").trigger('chosen:updated');
    $("#logiciel").chosen();
});

And my second one is (#ticket_type is another chosen) :

$(function () {
    $('#client').on('change', function () {
        var id = this.value;
        $("#logiciel").load('ajax/ticket_add_select.ajax.php?id=' + id);
        $("#logiciel").trigger('chosen:updated');
    });
});

Everything works fine when i select something on my first select, but on the first load, the second select is empty. Ajax is ok, i can see the result on log.

Is someone achieve to do something like that ?


回答1:


I'm guessing that you want to run these:

$("#logiciel").trigger('chosen:updated');
$("#logiciel").chosen();

After this completes:

$("#logiciel").load('ajax/ticket_add_select.ajax.php?id=' + $('#client').val());

Because .load() is asynchronous, you have to use the completion handler for the .load() call in order to know when it is done:

$(document).ready(function () {
    $("#logiciel").load('ajax/ticket_add_select.ajax.php?id=' + $('#client').val(), function() {
        $("#logiciel").trigger('chosen:updated').chosen();
    });
});

$(document).ready(function () {
    $('#client').on('change', function () {
        var id = this.value;
        $("#logiciel").load('ajax/ticket_add_select.ajax.php?id=' + id, function() {
            $("#logiciel").trigger('chosen:updated');
        });
    });
});


来源:https://stackoverflow.com/questions/34227162/chosen-ajax-on-ready

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!