简体   繁体   中英

Javascript: How to undo the removal of options using remove() within a dropdownlist?

I got two DropDownList: Min_Price & Max_Price and javascript that remove the option from both DropDownList if matched the condition. Eg: if $ 30,000 from DropDownList Min_Price has been selected, options with DropDownList Max_Price will be removed but without removing the default value Max. Price Max. Price option. Is there a way to undo the permanent removal of options? Is there any logical error I had made? Thank you.

 var minPrice = document.querySelector('[name="Min_Price"]'); var maxPriceOptions = document.querySelectorAll('[name="Max_Price"] option'); var maxPrice = document.querySelector('[name="Max_Price"]'); var minPriceOptions = document.querySelectorAll('[name="Min_Price"] option'); var reg = /[\\$\\,]/g; // Remove the lower value options from Max_Price DropDownList when a selected value in Min_Price DropdownList is greater than the Max_Price minPrice.addEventListener("change", function(e){ var v = +e.target.value.replace(reg, ""); Array.from(maxPriceOptions).forEach(el=>el.value!==" " &&+el.value.replace(reg, "")<=v && el.remove()) }); // Remove the greater value options from Min_Price DropDownList when a selected value in Max_Price DropdownList is lower than the Max_Price maxPrice.addEventListener("change", function(elems){ var vr = +elems.target.value.replace(reg, ""); Array.from(minPriceOptions).forEach(ele=>ele.value!==" " && +ele.value.replace(reg, "")>=vr && ele.remove()) }); 
 <select name="Min_Price"> <option value=" ">Min. Price</option> <option value="0">$ 0</option> <option value="10000">$ 10,000</option> <option value="20000">$ 20,000</option> <option value="30000">$ 30,000</option> <option value="40000">$ 40,000</option> <option value="50000">$ 50,000</option> </select> <select name="Max_Price"> <option value=" ">Max. Price</option> <option value="10000">$ 10,000</option> <option value="20000">$ 20,000</option> <option value="30000">$ 30,000</option> <option value="40000">$ 40,000</option> <option value="50000">$ 50,000</option> </select> 

Use the hidden property for the option element instead of removing!

 var minPrice = document.querySelector('[name="Min_Price"]'); var maxPriceOptions = document.querySelectorAll('[name="Max_Price"] option'); var maxPrice = document.querySelector('[name="Max_Price"]'); var minPriceOptions = document.querySelectorAll('[name="Min_Price"] option'); var reg = /[\\$\\,]/g; // Remove the lower value options from Max_Price DropDownList when a selected value in Min_Price DropdownList is greater than the Max_Price minPrice.addEventListener("change", function(e){ var v = +e.target.value.replace(reg, ""); Array.from(maxPriceOptions).forEach(el=> { el.value!==" " && el.value.replace(reg, "")<=v ? el.hidden = true : el.hidden = false;} ) }); // Remove the greater value options from Min_Price DropDownList when a selected value in Max_Price DropdownList is lower than the Max_Price maxPrice.addEventListener("change", function(elems){ var v = +elems.target.value.replace(reg, ""); Array.from(minPriceOptions).forEach(el=> { el.value!==" " && el.value.replace(reg, "")>=v ? el.hidden = true : el.hidden = false;} ) }); 
 <select name="Min_Price"> <option value=" ">Min. Price</option> <option value="0">$ 0</option> <option value="10000">$ 10,000</option> <option value="20000">$ 20,000</option> <option value="30000">$ 30,000</option> <option value="40000">$ 40,000</option> <option value="50000">$ 50,000</option> </select> <select name="Max_Price"> <option value=" ">Max. Price</option> <option value="10000">$ 10,000</option> <option value="20000">$ 20,000</option> <option value="30000">$ 30,000</option> <option value="40000">$ 40,000</option> <option value="50000">$ 50,000</option> </select> 

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