gpt4 book ai didi

Javascript:如何在下拉列表中使用 remove() 撤消选项的删除?

转载 作者:行者123 更新时间:2023-11-30 14:56:50 24 4
gpt4 key购买 nike

我有两个 DropDownList:Min_Price & Max_Price 和 javascript,如果符合条件,它会从两个 DropDownList 中删除选项。例如:如果从 DropDownList Min_Price 中选择了 $ 30,000,则带有 DropDownList Max_Price 的选项将被删除,但不会删除默认值 Max .价格选项。有没有办法撤消永久删除选项?我犯了任何逻辑错误吗?谢谢。

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>

最佳答案

对选项元素使用隐藏属性而不是移除!

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>

关于Javascript:如何在下拉列表中使用 remove() 撤消选项的删除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47123181/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com