【发布时间】:2017-10-10 01:24:58
【问题描述】:
假设,我有两个测验问题。每个问题都有用于选择答案的单选按钮和一个用于清除答案的明确答案按钮。 仅当在其各自的问题块中选择/取消选择答案时,清除按钮才会隐藏/显示。
如何确定选择/取消选择的单选按钮是否是其最近的问题块并相应地显示/隐藏清除按钮?
还有,这是我目前正在使用的 JS 代码
jQuery(function($) {
if ($("input[type]").is(":radio")) {
$(".answer").append("<button type='button' name='clear' >Clear</button>");
}
if ($("input[type=radio]:checked").length > 0) {
$('button[name=clear]').show();
}
else{
$('button[name=clear]').hide();
}
$("input[type=radio]").on('click', function() {
$nearest= $(this).closest('.answer').find('button[name=clear]');
$nearest.show();
});
$('button[name=clear]').on('click', function() {
$(this).closest('.answer').find(':radio').prop('checked', false);
$(this).hide();
});
if ($('.content').parent().hasClass("deferredfeedback")) {
if ($('.answer .r0 input').prop('disabled')) {
$("button[name=clear]").remove();
}
} else {
if (!($('div.im-controls').length)) {
$("button[name=clear]").remove();
}
}
});
我错过了一些我无法找到的东西,有人可以指导我吗?
【问题讨论】:
-
你能分享你的html吗?也许你可以只使用 CSS 来做到这一点。
-
您可以使用不同的想法来做到这一点,1)您可以将每个问题 + 按钮放在一个 div 中。因此,在单击按钮时,您可以获得父 div 并取消选择其中的所有单选按钮 2)为单选按钮和清除按钮提供一个棘手的 id 名称,因此在获取清除按钮 id 时,您可以找到与此相关的单选按钮。
标签: javascript jquery radio-button show-hide