jQuery 选择,以更新<选项>字段

jquery chosen, to update <option> fields

本文关键字:选项 字段 选择 更新 jQuery      更新时间:2023-09-26

有 HTML:

<form id='country'>
<select id="region" name="region" class='chosen'>
   <?=$regions;?>
</select>
<br>
<select id="city" name="city" class='chosen'></select>
<br>
<select id="district" name="district" class='chosen'></select>
<br>
<select id="village" name="village" class='chosen'></select>
</form>

我的选项获取 Ajax 的内容:

$(document).ready(function(){
$("#region").on("change", function(){
    $(this).nextAll('select').html('');
    $.ajax({
        type: "POST",
        url: "index.php",
        data: "ter_id="+$(this).val(),
        success:function(_ajax){
            $("#city").html(_ajax);
        }
    });
    $(this).trigger('chosen:updated'); //don't working, as like $("#country").trigger("liszt:updated")
})

我做错了什么?获取 ajax 数据后,我的选项不会更新。我需要写什么以及在哪里写?chosen_v1.2.0.

我不知道

选择是如何工作的,但它需要在成功回调中触发。在您的代码中,您在 ajax 响应可用之前就这样做了。

$(document).ready(function(){        
    $("#region").on("change", function(){
        var that = $(this);
        that.nextAll('select').html('');
        $.ajax({
            type: "POST",
            url: "index.php",
            data: "ter_id="+that.val(),
            success:function(_ajax){
                $("#city").html(_ajax);
                that.trigger('chosen:updated'); <<=== HERE
            }
        });
    });
});

我知道了。我只是在部分填充后成功应用 selectsen() 方法。

$("#region").on("change", function(){
    var that = this;
    $(this).nextAll('select').html('').addClass('hidden');
    $.ajax({
        type: "POST",
        url: "index.php",
        data: "ter_id="+$(this).val(),
        success:function(_ajax){
            $("#city").removeClass('hidden').attr('required','required').html(_ajax).chosen({'no_results_text': "Oops, nothing found!"});
        }
    });
})