gpt4 book ai didi

javascript - 选择和 If 语句

转载 作者:行者123 更新时间:2023-11-28 17:27:39 25 4
gpt4 key购买 nike

我将“Chosen”集成到了我的三个下拉菜单中。我创建了一个函数,因此如果在第一个下拉列表中进行任何选择,则会出现第二个下拉列表。然后,如果在第二个下拉列表中进行任何选择,则会出现第三个下拉列表。

我想更改我的代码,以便只有特定的选择才会触发下一个下拉列表的出现。例如,如果在第一个下拉列表中选择的值是“clean”,则将出现第二个下拉列表。总之,我想将触发器从任何值更改为特定值。我该怎么做呢?非常感谢!

<!DOCTYPE html>
<html>
<head>
<title>Experiment</title>
<link type="text/css" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.8.7/chosen.css">
<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.8.7/chosen.jquery.min.js"></script>
<script type="text/javascript">
$(function() {
$(".chosen-select").chosen({
disable_search_threshold: 4
});

function hide() {
$("#p1").parent().hide();
$("#p2").parent().hide();
}
// call hide AFTER .chosen() has been invoked on the visible elements
hide();
});
</script>
<script>
function onSelect1() {
$("#p1").parent().show();
}

function onSelect2() {
$("#p2").parent().show();
}
</script>
<style type="text/css">
.chosen-select {
width: 200px
}
</style>
</head>
<!-- Do not call hide() on body load -->
<body>
<table>
<tr>
<td onchange="onSelect1()">
<select class="chosen-select">
<option value="" disabled selected>Title</option>
<option value="clean">Clean</option>
<option value="salvage">Salvage</option>
<option value="rebuilt">Rebuilt</option>
</select>
</td>
<td onchange="onSelect2()">
<select class="chosen-select" id="p1">
<option value="" disabled selected>Title Status</option>
<option value="in hand">In Hand</option>
<option value="lien">Lien</option>
<option value="missing">Missing</option>
</select>
</td>
<td>
<select class="chosen-select" id="p2">
<option value="" disabled selected>Title Stat</option>
<option value="in hand">In Hand</option>
<option value="lien">Lien</option>
<option value="missing">Missing</option>
<option value="ralph jr.">Missing</option>
<option value="ralph">Missing</option>
</select>
</td>
</tr>
</table>
</body>
</html>

最佳答案

使用所选的更改功能

<!DOCTYPE html>
<html>

<head>
<title>Experiment</title>

<link type="text/css" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.8.7/chosen.css">

<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.8.7/chosen.jquery.min.js"></script>

<script type="text/javascript">
$(function() {
$(".chosen-select").chosen({
disable_search_threshold: 4
});

$("#p0").chosen().change(function(){
if ($(this).val() === 'clean'){
$("#p1").parent().show();
}
});


function hide() {
$("#p1").parent().hide();
$("#p2").parent().hide();
}
// call hide AFTER .chosen() has been invoked on the visible elements
hide();

});
</script>

<script>

function onSelect2(){
$("#p2").parent().show();
}
</script>


<style type="text/css">
.chosen-select {width:200px}
</style>
</head>
<!-- Do not call hide() on body load -->
<body>
<table>
<tr>
<td>
<select class="chosen-select" id="p0">
<option value="" disabled selected>Title</option>
<option value="clean">Clean</option>
<option value="salvage">Salvage</option>
<option value="rebuilt">Rebuilt</option>
</select>
</td>
<td onchange="onSelect2()">
<select class="chosen-select" id="p1" >
<option value="" disabled selected>Title Status</option>
<option value="in hand">In Hand</option>
<option value="lien">Lien</option>
<option value="missing">Missing</option>
</select>
</td>
<td>
<select class="chosen-select" id="p2">
<option value="" disabled selected>Title Stat</option>
<option value="in hand">In Hand</option>
<option value="lien">Lien</option>
<option value="missing">Missing</option>
<option value="ralph jr.">Missing</option>
<option value="ralph">Missing</option>
</select>
</td>
</tr>
</table>




</body>

</html>

关于javascript - 选择和 If 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51198500/

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