简体   繁体   中英

jQuery function called multiple times, only remove once

I have a function that removes all of the options from a drop down box except for the first one. This function is updated every time another drop down box is changed, and appends other elements depending on what the other drop down is selected. My problem is I always want to keep the first item, but it removes it whenever I call the function more than the first time. What is a better way of doing this?

function updateMountingMethod(){

 var selectedVal = $('#attrib-13 option:selected').text().split(" ")[0];
 var optionChildren = $("#attrib-7").children(":not(:first-child)").clone();

 $("#attrib-7").children(":not(:last-child)").remove();


  $.each(optionChildren, function(){


  if ($(this).text().split('|')[1]!=undefined)
   {
   var values = $(this).text().split('|')[1];

   if(values.substring(1,3) == selectedVal)
   {

    $("#attrib-7").append($(this).text($(this).text().split("|")[0]+" "+$(this).text().split("|")[2]));
   }
   }


 })
}
document.getElementByID('attrib-7').options.length = 1;

this removes all but the first option from the select list. not sure the JQuery equivelent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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