jQuery Gets the page li array and deletes key that is not in the array

  • 2021-07-07 06:32:36
  • OfStack

Let me tell you the application scenarios first:

Get the value of key under the page li and add it to the arr array

Delete the value value where the vehicle type is not in the arr array.

Sample code:


var getSaleModels = function(brand_id){
if(brand_id){
var li = $(".pinpai-chexing"+brand_id+" li");
var arr = new Array();
if (li.length > 0){
li.each(function(index, element) {
arr.push($(element).children("a").attr("data-value"));
});
}
}
return arr;
}

Application


// Acquisition of vehicle type 
var getModel = function (id){
var arr = getSaleModels($("#brand_id").attr("data-value"));
$.getJSON("/interface/CarBase/getTuangouModels/",{id:$("#brand_id").attr("data-value")},function(json){
var models = $(".models");
var option = "";
$(models).find("li").empty(); // Empty the original options 
if(json){
$.each(json.data,function(index,array){
// Delete discontinued models 
if($.inArray(array['model_id'],arr)!=-1){
if(!index){
$("#model_id").attr("data-value",array['model_id']);
$("#model_id").text(array['model']);
}
option += "<li data-value='"+array['model_id']+"'";
option += ">"+array['model']+"</li>";
}
});
models.append(option); 
}
});
return false;
}

Related articles: