根据下拉列表 1 中选定的选项显示/隐藏下拉列表 2 或下拉列表 3

发布于 2025-01-11 08:59:15 字数 2360 浏览 0 评论 0原文

我的表单中有 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>

我需要根据第一个下拉菜单的选择来显示/消失第二个和第三个下拉菜单。 2 和 3 必须在页面加载时隐藏,或者在下拉列表 1 中选择值 3 时隐藏,但不仅仅是从视图中隐藏,而是完全不存在。我这样说是因为 jquery .show 和 .hide 只会使元素从显示中消失,它仍然保留在代码中,并且由于这些隐藏下拉列表中的“required”属性,表单无法提交。

我已经尝试过这个以及我找到的许多其他答案,但没有运气...

<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>

请帮助...

编辑: 类似的事情是:

<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();
}
})

我不太擅长jquery。这就是我想要的,但只有一次。我不知道一旦移除后如何将它们带回来。例如,如果我从#1:1(汽车)和#2:1(小型车)中选择,则#3 将被删除。但如果我现在决定在#1 上选择另一个选项,什么也不会发生。另外,加载时,所有 3 个都会显示,我想保持 #2 和 #3 隐藏,直到选择 #1 上的选项。

I have 3 dropdowns in a 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>

<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. 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.

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. 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. But if i now decide to choose another option on #1 nothing happens. Also, on load, all 3 are shown, I wanted to keep #2 and #3 hidden until an option on #1 is selected.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晨光如昨 2025-01-18 08:59:15

这是一种解决方法。为什么不使用数据属性,而不是使用数字作为 ID。每次选择一个选项时,代码都会显示序列中的下一个选项。

$(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>

Here's one way to go about it. Instead of using numbers as ID's, why not use a data-attribute. Each time a select option is chosen the code will show the next one in the sequence.

$(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>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文