简体   繁体   English

选择了jquery,以进行更新 <option>领域

[英]jquery chosen, to update <option> fields

Have HTML: 有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>

My optionsget content by Ajax: 我的选项通过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")
})

What am I doing wrong? 我究竟做错了什么? My options don't update after getting ajax-data. 获取ajax数据后,我的选项不会更新。 What and where need I to write? 我需要在什么地方写? chosen_v1.2.0. chosen_v1.2.0。

I don't know how chosen works but it needs to be triggered in the success callback. 我不知道选择的工作方式,但是需要在成功回调中触发它。 In your code, you're doing it way before the ajax response is available. 在您的代码中,您正在这样做,直到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
            }
        });
    });
});

I've got it. 我懂了。 I just apply a chosen() method in success, after section filling. 我只成功应用所选择的()方法,后段填充。

$("#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!"});
        }
    });
})

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

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