How to clear parameter values ​​under select tag - html

I am using two dropdowns (first dropdown category and second dropdown subcategory) on the page, both dropdowns will load dynamically in which I will select a value from the first dropdown accordingly I need to load the value into the second dropdown menu. I did it, but the point is, it will be executed the first time.

But when I click on any other option in the states dropdown, it doesn't update in the second dropdown.

And my code: This piece of code is for getting a list of categories on page load, i.e. in document.ready

$.ajax({
    url : "../category/getCategory",
    type : "post",
    contentType : "application/json",
    dataType : "json",
    success : function(data) {
        var categoryBOs = data.categoryBOs;
        $.each(categoryBOs, function(key, value) {
            $("#productCategory").append(
                    '<option value='+value.categoryId+'>'
                            + value.categoryName
                            + '</option>');
        });
    }

});

      

This ajax part is for loading subcategory

$("#productCategory").on('change', function() {
    alert($(this).val());
    $.ajax({
        url : "../category/getSubCategory",
        type : "post",
        cache : false,
        dataType : "json",
        data : "categoryId=" + $(this).val(),
        success : function(data) {
            var subCategoryBOs = data.subCategoryBOs;

            $.each(subCategoryBOs, function(key, subCategoryBO) {
                subCategories.push({lable:subCategoryBO.categoryId , value:subCategoryBO.categoryName});
                 $("#productSubCategory").append(
                        '<option value='+subCategoryBO.categoryId+'>'
                                + subCategoryBO.categoryName
                                + '</option>'); 
            });
        }
    });
});

      

+3


source to share


4 answers


Try adding $("#productCategory").empty()

before the first $.each

and $("#productSubCategory").empty()

before the second $.each

.



0


source


From what I can see in your code, you always add new entries, but never remove old ones. So maybe your list just keeps working with new entries at the end? Try deleting entries before adding new ones:

$("#productSubCategory option").remove();
$("#productSubCategory").append(
    '<option value=' + subCategoryBO.categoryId + '>' + subCategoryBO.categoryName + '</option>');

      



In my experience, $ .each with $ .append can get very slow with some number of entries in the list. I would rewrite it in my own javascript using for () and createElement ().

0


source


you need to do html

in .each

and add after the .each

end. The first time you disable the option, you shouldn't delete the previous one using$("#productSubCategory option").remove();

$.ajax({
    url: "../category/getCategory",
    type: "post",
    contentType: "application/json",
    dataType: "json",
    success: function (data) {
        var categoryBOs = data.categoryBOs;
        var html = '';
        $.each(categoryBOs, function (key, value) {
            html += '<option value=' + value.categoryId + '>' + value.categoryName + '</option>';
        });

        $("#productCategory").append(html);
    }

});  


$("#productCategory").on('change', function () {
    alert($(this).val());
    $.ajax({
        url: "../category/getSubCategory",
        type: "post",
        cache: false,
        dataType: "json",
        data: "categoryId=" + $(this).val(),
        success: function (data) {
            var subCategoryBOs = data.subCategoryBOs;
            var html = '';
            $.each(subCategoryBOs, function (key, subCategoryBO) {
                subCategories.push({ lable: subCategoryBO.categoryId, value: subCategoryBO.categoryName });
                html += '<option value=' + subCategoryBO.categoryId + '>' + subCategoryBO.categoryName + '</option>';
            });
            $("#productSubCategory").append(html);
        }
    });
});

      

0


source


You just have to do here that every time you load subcategories, just specify before that.

 $(dropdown).empty();

      

Thank!

0


source







All Articles