JQuery AJAX Dropdown反复重置

时间:2015-10-14 15:10:58

标签: javascript jquery html ajax

从这个问题:JQuery Populate Dropdown by Attribute of tr tag

还有另一种方法可以实现这一目标吗?我无法找到此

背后的算法

JQuery Code VehicleCategory():

function VehicleCategory(){

            $("#category" + counterVehicle).empty();
            $("#category" + counterVehicle).append("<option>Loading.....</option>");
            $("#brand"  + counterVehicle).append("<option value=''>-Select Brand-</option>");
            $("#model" + counterVehicle).append("<option value=''>-Select Model-</option>");
            $("#year" + counterVehicle).append("<option value=''>-Select Year-</option>");
            $("#mileage" + counterVehicle).append("<option value=''>-Select Mileage-</option>");
            $.ajax({
                type:"POST",
                url:"car_dropdown.php?comboBoxName=category",
                contentType:"application/json; charset=utf-8",
                dataType:"json",
                success: function(data){
                    $("#category" + counterVehicle).empty();
                    $("#category" + counterVehicle).append("<option value=''>-Select Category-</option>");
                    $.each(data,function(i,item){
                         $("#category" + counterVehicle).append('<option value="'+ data[i].category +'">'+ data[i].category +'</option>');
                    });

                },

                complete: function(){

                }

            });
        }

加载页面时: enter image description here

当我点击第一个下拉列表时: enter image description here

当我再次点击下拉菜单时^^&#39;: enter image description here

2 个答案:

答案 0 :(得分:0)

您多次调用VehicleCategory()..如果您需要这样做,请清空之前的内容..

改变..

$("#brand"  + counterVehicle).append("<option value=''>-Select Brand-</option>");

为:

$("#brand"  + counterVehicle).html("<option value=''>-Select Brand-</option>");

答案 1 :(得分:0)

您需要将counterVehicle与将其置于更改事件上... ...

这是一个例子,您需要更改它们..

这不是你的代码,而是文盲你需要做的事情。

html - 这里的div将是你的tr或者什么......

<div id ="1234">
    <select class="category">
      <option value="volvo">Volvo</option>
      <option value="saab">Saab</option>
      <option value="mercedes">Mercedes</option>
      <option value="audi">Audi</option>
    </select>
</div>

的javascript

$('document').ready(function(){

    $(".category").change(function(event){
        var $this = $(event.target);

        var selectValue = $this.val();
        console.log(selectValue);
        //so in your case you would traverse the dom to tr and then do...
        //something like this part is not tested...
        var div = $this.closest('div');
        var divId = div.attr("id");
        console.log(divId);

    });

});