【发布时间】:2013-05-27 07:29:34
【问题描述】:
我有这个 jQuery:
$(document).ready(function() {
$("input[type=radio]").change(function()
{
var divId = $(this).attr("id");
if ($(this).is(":checked")) {
$("." + divId).show();
}
else {
$("." + divId).hide();
}
});
$("input[type=radio]").change();
});
div 应该显示/隐藏取决于输入是否被选中/取消选中
编辑:我应该提到,使用复选框,div 会显示和隐藏,但使用收音机它们只会显示 :(。
它适用于复选框,但不适用于单选按钮,这是为什么呢?
【问题讨论】:
-
$("." + divId).show();你选择的是一个类,而不是一个 id。使用#而不是. -
div 的类与单选框/复选框 id 相同
-
啊,我想如果你会添加 HTML,我会知道的。
-
The problem is that the currently selected radio button doesn't receive a change event when a new radio button from the group is selected.只有新选择的单选按钮会收到更改事件。