如何使用 jQuery 根据单选按钮选择显示/隐藏 div?
我有一些单选按钮,我希望根据选择的单选按钮显示不同的隐藏 div。 HTML 如下所示:
<form name="form1" id="my_form" method="post" action="">
<div><label><input type="radio" name="group1" value="opt1">opt1</label></div>
<div><label><input type="radio" name="group1" value="opt2">opt2</label></div>
<div><label><input type="radio" name="group1" value="opt3">opt3</label></div>
<input type="submit" value="Submit">
</form>
....
<style type="text/css">
.desc { display: none; }
</style>
....
<div id="opt1" class="desc">lorem ipsum dolor</div>
<div id="opt2" class="desc">consectetur adipisicing</div>
<div id="opt3" class="desc">sed do eiusmod tempor</div>
这是我的 jQuery:
$(document).ready(function(){
$("input[name$='group2']").click(function() {
var test = $(this).val();
$("#"+test).show();
});
});
我这样做的原因是因为我的单选按钮和 div 是动态生成的(单选按钮的值将始终有一个相应的 div)。上面的代码部分工作 - 当选中正确的按钮时,div 将显示,但我需要添加一些代码,以便在取消选中按钮后使 div 再次隐藏。谢谢!
I have some radio buttons and I'd like to have different hidden divs show up based on which radio button is selected. Here's what the HTML looks like:
<form name="form1" id="my_form" method="post" action="">
<div><label><input type="radio" name="group1" value="opt1">opt1</label></div>
<div><label><input type="radio" name="group1" value="opt2">opt2</label></div>
<div><label><input type="radio" name="group1" value="opt3">opt3</label></div>
<input type="submit" value="Submit">
</form>
....
<style type="text/css">
.desc { display: none; }
</style>
....
<div id="opt1" class="desc">lorem ipsum dolor</div>
<div id="opt2" class="desc">consectetur adipisicing</div>
<div id="opt3" class="desc">sed do eiusmod tempor</div>
And here's my jQuery:
$(document).ready(function(){
$("input[name$='group2']").click(function() {
var test = $(this).val();
$("#"+test).show();
});
});
The reason I'm doing it that way is because my radio buttons and divs are being generated dynamically (the value of the radio button will always have a corresponding div). The code above works partially - the divs will show when the correct button is checked, but I need to add in some code to make the divs hide again once the button is unchecked. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
更新 2015/06
随着 jQuery 自问题发布以来不断发展,现在推荐的方法是使用
$.on
或外部
$.ready()
原始答案
您应该使用
.change()
事件处理程序:应该可以工作
Update 2015/06
As jQuery has evolved since the question was posted, the recommended approach now is using
$.on
or outside
$.ready()
Original answer
You should use
.change()
event handler:should work
在显示之前隐藏它们:
Just hide them before showing them:
在此示例中,
input[name=group1]
是正确的。不过,感谢您的代码!It's correct
input[name=group1]
in this example. However, thanks for the code!一个有趣的解决方案是使其成为声明式的:您只需为每个应该显示的 div 提供一个属性
automaticallyVisibleIfIdChecked
以及它所依赖的复选框或单选按钮的 id。也就是说,您的表单如下所示:并且具有一些独立于页面的 JavaScript,可以很好地使用函数式编程:
类似地,您可以使用属性
automaticallyEnabledIfChecked
自动启用/禁用表单字段。我认为这种方法很好,因为它避免了为您的页面创建特定的 JavaScript - 您只需插入一些说明应该做什么的属性。
An interesting solution is to make this declarative: you just give every div that should be shown an attribute
automaticallyVisibleIfIdChecked
with the id of the checkbox or radio button on which it depends. That is, your form looks like this:and have some page independent JavaScript that nicely uses functional programming:
Similarily you could automatically enable / disable form fields with an attribute
automaticallyEnabledIfChecked
.I think this method is nice since it avoids having to create specific JavaScript for your page - you just insert some attributes that say what should be done.
相同的简单 jquery 源 -
为了使其更合适,只需将选中添加到第一个选项 -
从样式中删除 .desc 类并修改 div 之类 -
无论如何,它看起来确实不错。
The simple jquery source for the same -
In order to make it little more appropriate just add checked to first option --
remove .desc class from styling and modify divs like --
it will really look good any-ways.
您可以使用 jQuery 的
show()
和hide()
方法。如下所示:JQuery:
HTML:
这是一个示例:根据单选按钮点击显示/隐藏 DIV
You can use jQuery’s
show()
andhide()
methods. Like below:JQuery:
HTML:
Here is an example: Show/hide DIV based on Radio Button click
下面的代码非常适合我:
Below code is perfectly workd for me:
我写了一个简单的代码来了解如何在 jquery 中显示和隐藏单选按钮,它非常简单
Jquery 代码
给我评论
I wrote a simple code to unterstand you to how to make a show and hide radio buttons in jquery its very simple
Jquery code
give me comments