jQuery / Coffeescript隐藏除$(this)之外的相关名称,不允许我勾选选择框

jQuery / Coffeescript hide related names except $(this), won't allow me to check the select box

本文关键字:不允许 允许我 选择 隐藏 Coffeescript this jQuery      更新时间:2023-09-26

我有一些元素,它们是名称中有"correct_answer"的选择框。我想选择一个框,并隐藏页面上的所有其他框。当我用当前代码单击给定的选择框时,它会像我想要的那样隐藏除我单击的那个框之外的所有其他框,但是随后复选框不会显示在框中,并且框不再起作用。有什么建议吗?以下是我在coffeescript中的代码:

jQuery ->
$('input[name*="correct_answer"]').on "click", (event) ->
    $('input[name*="correct_answer"]').hide()
    $(this).show()
    event.preventDefault()

因为您调用的是preventDefault(),所以点击的默认动作是改变复选框的选中状态,最好使用change事件

jQuery ->
$('input[name*="correct_answer"]').on "change", (event) ->
    $('input[name*="correct_answer"]').hide()
    $(this).show()

jQuery ->
    $checks = $('input[name*="correct_answer"]').on "change", (event) ->
        $checks.not(this).hide()

演示:小提琴