如何通过选择显示/隐藏div。(jquery)

发布于 2024-07-25 09:08:36 字数 526 浏览 5 评论 0原文

我的代码:

<select id="select">
<option id="1" value="thai language">option one</option>
<option id="2" value="eng language">option two</option>
<option id="3" value="other language">option three</option>
</select>

<div id="form1">content here</div>
<div id="form2">content here</div>
<div id="form3">content here</div>

我想要的是在选择选项 1 时显示 div #form1 并隐藏 form2+form3,或者 选择选项 2 显示 div#form2 并隐藏 form1+form2

my code:

<select id="select">
<option id="1" value="thai language">option one</option>
<option id="2" value="eng language">option two</option>
<option id="3" value="other language">option three</option>
</select>

<div id="form1">content here</div>
<div id="form2">content here</div>
<div id="form3">content here</div>

what i want is to show div #form1 when select option 1 and hide form2+form3, or
select option 2 show div#form2 and hide form1+form2

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

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

发布评论

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

评论(5

北方的韩爷 2024-08-01 09:08:37

如果您的表单很大,您可以将它们放入单独的文件中,如下所示,

$(document).ready(function() {
     $('#select').change(function() {
         $("#myform").load(this.value);
     });
 });


<select id="select">
<option value="blank.htm">Select A Form</option>
<option value="test1.htm">option one</option>
<option value="test2.htm">option two</option>
<option value="test3.htm">option three</option>
</select>

<div id="myform" ></div>

If your forms are large, you can put them in separate files like this,

$(document).ready(function() {
     $('#select').change(function() {
         $("#myform").load(this.value);
     });
 });


<select id="select">
<option value="blank.htm">Select A Form</option>
<option value="test1.htm">option one</option>
<option value="test2.htm">option two</option>
<option value="test3.htm">option three</option>
</select>

<div id="myform" ></div>
愿与i 2024-08-01 09:08:37

像这样的东西吗?

var optionValue = $("#select").val();

$('#form1, #form2, #form3').hide();

switch(optionValue)
{
case 1:
  $("#form1").show();
  break;
case 2:
  $("#form2").show();
  break;
case: 3:
  $("#form3").show();
  break;
}

Something like this?

var optionValue = $("#select").val();

$('#form1, #form2, #form3').hide();

switch(optionValue)
{
case 1:
  $("#form1").show();
  break;
case 2:
  $("#form2").show();
  break;
case: 3:
  $("#form3").show();
  break;
}
初心未许 2024-08-01 09:08:37

只隐藏之前显示的 div 不是更好吗?
所以;

var selection = 0;
$('#select').change(function() {
  $('#form' + selection).hide();
  selection = $(this).val();
  $('#form' + selection).show();
});

请注意,ID 不应以数字开头,但上面的内容应该可以。

Wouldn't it be better to only hide the previously shown div?
So;

var selection = 0;
$('#select').change(function() {
  $('#form' + selection).hide();
  selection = $(this).val();
  $('#form' + selection).show();
});

Do note that IDs should not start with numbers, but the above should do it.

鲜肉鲜肉永远不皱 2024-08-01 09:08:37

更好的版本:

$('#select').change(function() {
   $('div').not('#form' + $(this).find('option:selected').attr('id')).hide();
   $('#form' + $(this).find('option:selected').attr('id')).show();
});

Better version:

$('#select').change(function() {
   $('div').not('#form' + $(this).find('option:selected').attr('id')).hide();
   $('#form' + $(this).find('option:selected').attr('id')).show();
});
热风软妹 2024-08-01 09:08:36
$('#select').change(function() {
   $('#form1, #form2, #form3').hide();
   $('#form' + $(this).find('option:selected').attr('id')).show();
});

请注意,ID 不应以数字开头,但上面的内容应该可以。

$('#select').change(function() {
   $('#form1, #form2, #form3').hide();
   $('#form' + $(this).find('option:selected').attr('id')).show();
});

Do note that IDs should not start with numbers, but the above should do it.

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