Program Club

jQuery Chosen 드롭 다운 목록 지우기 및 새로 고침

proclub 2020. 10. 28. 21:15
반응형

jQuery Chosen 드롭 다운 목록 지우기 및 새로 고침


jQuery Chosen 드롭 다운 목록을 지우고 새로 고치려고합니다.

HTML :

<select data-placeholder="Select Picture..." class="chosen-select" style="width:250px;" id="picturegallery" tabindex="2">
    <option value="" selected="selected"></option>
    <option value="x">remove me</option>
</select>

"새로 고침"버튼을 클릭하면 다음과 같이 바뀝니다.

<select data-placeholder="Select Picture..." class="chosen-select" style="width:250px;" id="picturegallery" tabindex="2">
    <option value="1">test</option>
</select>

내가 시도한 것 :

$("#refreshgallery").click(function(){
    $('#picturegallery').empty();
    var newOption = $('<option value="1">test</option>');
    $('#picturegallery').append(newOption);
});

하지만 드롭 다운 목록을 업데이트 할 수 없습니다. 도움이 필요하십니까? :)


를 사용 .trigger("chosen:updated");하면 추가 한 후 옵션 목록을 업데이트 할 수 있습니다.

선택한 항목을 동적으로 업데이트 : 선택 필드의 옵션을 업데이트해야하고 Chosen이 변경 사항을 적용하도록하려면 필드에서 "chosen : updated"이벤트를 트리거해야합니다. 선택한 콘텐츠는 업데이트 된 콘텐츠를 기반으로 자체적으로 다시 빌드됩니다.

코드 :

$("#refreshgallery").click(function(){
        $('#picturegallery').empty(); //remove all child nodes
        var newOption = $('<option value="1">test</option>');
        $('#picturegallery').append(newOption);
        $('#picturegallery').trigger("chosen:updated");
    });

경우 trigger("chosen:updated");작동하지 않는, 사용 .trigger("liszt:updated");@Nhan 트란 그것의 벌금을 노력하고 있습니다.


$("#idofBtn").click(function(){
        $('#idofdropdown').empty(); //remove all child nodes
        var newOption = $('<option value="1">test</option>');
        $('#idofdropdown').append(newOption);
        $('#idofdropdown').trigger("chosen:updated");
    });

MVC 4 :

    function Cargar_BS(bs) {
        $.getJSON('@Url.Action("GetBienServicio", "MonitoreoAdministracion")',
                        {
                            id: bs
                        },
                        function (d) {
                            $("#txtIdItem").empty().append('<option value="">-Seleccione-</option>');
                            $.each(d, function (idx, item) {
                                jQuery("<option/>").text(item.C_DescBs).attr("value", item.C_CodBs).appendTo("#txtIdItem");
                            })
                            $('#txtIdItem').trigger("chosen:updated");
                        });
    }

In my case, I need to update selected value at each change because when I submit form, it always gets wrong values and I used multiple chosen drop downs. Rather than updating single entries, change selector to update all drop downs. This might help someone

 $(".chosen-select").chosen().change(function () {
    var item = $(this).val();
    $('.chosen-select').trigger('chosen:updated');
});

참고URL : https://stackoverflow.com/questions/20148195/clear-and-refresh-jquery-chosen-dropdown-list

반응형