jQuery .show() 和 .hide() 用于克隆的单选按钮

jQuery .show() and .hide() for cloned radio buttons

我有一系列克隆的单选按钮。我想做一件简单的事情,如果选择了相关的无线电,则显示一个特定的 div,如果没有,则隐藏。这是 jQuery 函数:

$clone.find('[id^="mov"]').each(function(){
    $(this).click(function(){
            $clone.find('[id^="l"]').each(function(){
            if ($(this).is(":checked")){
                $clone.find('[id^="linear"]').each(function(){
                $(this).show();
                });
            } 
            });
            $clone.find('[id^="c"]').each(function(){
            if ($(this).is(":checked")){
                $clone.find('[id^="circular"]').each(function(){
                $(this).show();
                });
            } 
            });
            $clone.find('[id^="r"]').each(function(){
            if ($(this).is(":checked")){
                $clone.find('[id^="rotacional"]').each(function(){
                $(this).show();
                });
            } 
            });
            $clone.find('[id^="m"]').each(function(){
            if ($(this).is(":checked")){
                $clone.find('[id^="mudanca_config_mao"]').each(function(){
                $(this).show();
                });
            }
            }); 
    });
});

我设法显示了我想要的 div,但我似乎无法隐藏它们。当我为其他人做同样的事情时,它不会工作并停止显示 div.

if ($(this).is(":checked")){
    $clone.find('[id^="linear"]').each(function(){
    $(this).show();
    });
}
else {
    $clone.find('[id^="linear"]').each(function(){
    $(this).hide();
    });
}

另外,我知道这个函数有点重复和混乱,所以如果有人有更好更干净的方法,我愿意接受建议。我是 jQuery 的新手,还在学习 :) 非常感谢!

不看您的 html 就很难看出问题所在。不过,你JavaScript绝对可以收拾了。下面应该等同于你问题中的JavaScript。

$clone.find('[id^="mov"]').on('click', function(){
    $clone.find('[id^="linear"]').toggle(
        $clone.find('[id^="l"]').is(':checked'));
    $clone.find('[id^="circular"]').toggle(
        $clone.find('[id^="c"]').is(':checked'));
    $clone.find('[id^="rotacional"]').toggle(
        $clone.find('[id^="r"]').is(':checked'));
    $clone.find('[id^="mudanca_config_mao"]').toggle(
        $clone.find('[id^="m"]').is(':checked'));
});