简体   繁体   English

下拉动态django ajax

[英]Dropdown dynamic in django ajax

I am using django for dynamic dropdown.我正在使用 django 进行动态下拉。 There are two dropdown when the first dropdown was click and there is a subcategory for it, the second dropdown will show options.单击第一个下拉菜单时有两个下拉菜单,并且有一个子类别,第二个下拉菜单将显示选项。 I want to disable the second dropdown if there is no subcategory for it.如果没有子类别,我想禁用第二个下拉菜单。 How can I do that?我怎样才能做到这一点?

$("#id_general-collision_type").change(function () {
const url = $("#form_incidentgeneral").attr("data-acc-url");  // get the url of the `load_cities` view
const collisionId = $(this).val();  // get the selected country ID from the HTML input

$.ajax({                       // initialize an AJAX request
    url: url,                    // set the url of the request (= /persons/ajax/load-cities/ )
    data: {
        'collision_type_id': collisionId       // add the country id to the GET parameters
    },
    success: function (data) {  
        //console.log(data) // `data` is the return of the `load_cities` view function
        $("#id_general-collision_subcategory").html(data);  // replace the contents of the city input with the data that came from the server

        let html_data = '<option value="">---------</option>';
        data.forEach(function (collision_subcategory) {
            html_data += `<option value="${collision_subcategory.id}">${collision_subcategory.sub_category}</option>`
        });
        console.log(html_data);
        $("#id_general-collision_subcategory").html(html_data);

    }
});

}); });

success: function (data) {
    let select_element = $('select'); #Sub_category select
    $(select_element).html(''); #Make Empty Select
    for(let i=;i<data.length;i++){
         let x = data[i];
         let option_element = `<option value='${x['id']}'`>${x['sub_category']}</option>`;
         $(select_element).append(option_element);
   }
}

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

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