简体   繁体   English

根据从下拉菜单 1 中选择的选项显示/隐藏下拉菜单 2 或下拉菜单 3

[英]Show/hide dropdown 2 or dropdown 3 based on selected option from dropdown 1

I have 3 dropdowns in a form:我在一个表单中有 3 个下拉菜单:

<select id="1" required>
<option value="">Select type</option>
<option value="1">Car</option>
<option value="2">Truck</option>
<option value="3">Some other option</option>
</select>

<select id="2" required>
<option value="">Select option</option>
<option value="1">Small Car</option>
<option value="2">Big Car</option>
</select>

<select id="3" required>
<option value="">Select option</option>
<option value="1">Small Truck</option>
<option value="2">Big Car</option>
</select>

I need the second and third dropdown to appear/disappear based on selection of the first dropdown.我需要第二个和第三个下拉菜单根据第一个下拉菜单的选择出现/消失。 2 and 3 have to be hidden on page load, or when value 3 is selected on dropdown 1, but not just hidden from view, rather completely non-existant. 2 和 3 必须在页面加载时隐藏,或者当在下拉列表 1 中选择值 3 时,但不仅仅是从视图中隐藏,而是完全不存在。 I say this because jquery.show and.hide only makes an element disappear from display, it still stays inside the code and because of the "required" attribute inside those hidden dropdowns, form cannot submit.我这样说是因为 jquery.show and.hide 只会让一个元素从显示中消失,它仍然留在代码中,并且由于那些隐藏的下拉列表中的“必需”属性,表单无法提交。

I have tried this as well as many other answers I found, but had no luck...我已经尝试过这个以及我找到的许多其他答案,但没有运气......

<script>
$("#1").change(function () {
if ($(this).val() == '1') {
$('#2').show();
$('#3').hide();
} else if ($(this).val() == '2') {
$('#3').show();
$('#2').hide();
} else {
$("#2").hide();
$('#3').hide();
}
})
</script>

Please help...请帮忙...

Edit: Something along those lines:编辑:沿着这些线的东西:

<form id="form">
  <select id="1" required>
<option value="">Select type</option>
<option value="1">Car</option>
<option value="2">Truck</option>
<option value="3">Some other option</option>
</select>
<div id="here">
<select id="2" required>
<option value="">Select option</option>
<option value="1">Small Car</option>
<option value="2">Big Car</option>
</select>

<select id="3" required>
<option value="">Select option</option>
<option value="1">Small Truck</option>
<option value="2">Big Car</option>
</select>
  </div>
</form>


$("#1").change(function () {
if ($(this).val() == '1') {
$('#2').appendTo( "#here" );
 $('#3').remove();
} else if ($(this).val() == '2') {
$('#3').appendTo( "#here" );
 $('#2').remove();
} else {
  $('#2').remove();
  $('#3').remove();
}
})

I am not very good at jquery. This does what I want, but only once.我不太擅长 jquery。这就是我想要的,但只有一次。 I dont know how to bring them back once removed.我不知道一旦移除如何将它们带回来。 For example, if I selected from #1: 1(Car) and from #2: 1(Small Car), #3 will be removed.例如,如果我从#1: 1(Car) 和#2: 1(Small Car) 中选择,#3 将被删除。 But if i now decide to choose another option on #1 nothing happens.但是,如果我现在决定在 #1 上选择另一个选项,则什么也不会发生。 Also, on load, all 3 are shown, I wanted to keep #2 and #3 hidden until an option on #1 is selected.此外,在加载时,显示所有 3 个,我想隐藏 #2 和 #3,直到选择 #1 上的选项。

Here's one way to go about it.这是有关它的go的一种方法。 Instead of using numbers as ID's, why not use a data-attribute .与其使用数字作为 ID,不如使用data-attribute Each time a select option is chosen the code will show the next one in the sequence.每次选择 select 选项时,代码将显示序列中的下一个。

 $(document).ready(() => { $('select[data-order=1]').show() let selects = $('select[data-order]'); selects.change(function() { // get number let num = +$(this).data('order'); // show the next select and hide all selects after that selects.each(function(i,o) { let thisNum = +$(o).data('order'); if (thisNum === num + 1) $(o).show().val(''); // show and reset the value else if (thisNum > num + 1) $(o).hide(); }) }) })
 select[data-order] { display: none; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form id="form"> <select data-order='1' id="1" required> <option value="">Select type</option> <option value="1">Car</option> <option value="2">Truck</option> <option value="3">Some other option</option> </select> <select data-order='2' id="2"> <option value="">Select option</option> <option value="1">Small Car</option> <option value="2">Big Car</option> </select> <select data-order='3' id="3"> <option value="">Select option</option> <option value="1">Small Truck</option> <option value="2">Big Car</option> </select> </form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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