gpt4 book ai didi

javascript - 在 jQuery 中显示/隐藏多个选项

转载 作者:行者123 更新时间:2023-11-28 15:41:30 25 4
gpt4 key购买 nike

我需要在单选下拉列表中显示/隐藏 100 多个选项。到目前为止我的代码如下所示。然而,它似乎适用于少量选项,但不适用于大量选项。有更好的办法吗?

$(document).ready(function() {
$("#colors").change(function(){
var selectedValue = $(this).val();
url = "http://domain.com";
if(selectedValue == "color1"){
$("table#color1").show();
$("table#color2").hide();
$("table#color3").hide();
$.get(url, {name: "color1"});
}
if(selectedValue == "color2"){
$("table#color1").hide();
$("table#color2").show();
$("table#color3").hide();
$.get(url, {name: "color2"});
}
if(selectedValue == "color3"){
$("table#color1").hide();
$("table#color2").hide();
$("table#color3").show();
$.get(url, {name: "color3"});
}
});
});

最佳答案

为所有表格指定一个类(例如 class="colored-table")并执行以下操作:

$('table.colored-table').hide();
$('table#' + selectedValue).show();
// 'table#' + selectedValue : will become 'table#color1' if selectedValue equals 'color1' and so on
$.get(url, {name: selectedValue});

(编辑:忘记了 $.get)

关于javascript - 在 jQuery 中显示/隐藏多个选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23615734/

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