准备好选择ajax [英] Chosen ajax on ready

查看:80
本文介绍了准备好选择ajax的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用通过ajax调用选择的jquery: https://harvesthq.github.io/chosen/

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

我的html是:

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

我的第一个脚本是:

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

我的第二个是(另一个选择的是#ticket_type):

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');
    });
});

当我在第一次选择时选择某项时,一切正常,但是在第一次加载时,第二次选择为空. Ajax可以,我可以在日志中看到结果.

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.

有人做到了那样吗?

推荐答案

我猜您要运行这些代码:

I'm guessing that you want to run these:

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

完成后:

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

由于.load()是异步的,因此必须对.load()调用使用完成处理程序才能知道何时完成:

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');
        });
    });
});

这篇关于准备好选择ajax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆