用于多个表单元素的相同 jQuery 函数。我该如何巩固

发布于 2024-09-09 00:39:05 字数 624 浏览 3 评论 0原文

如果选中多个复选框,我需要显示和隐藏 div。

需要显示的每个 div 的 id 将与复选框名称相同。有没有一种简单的方法可以完成此任务,而无需编写函数来处理每个复选框?

    $('input[name=checkbox_1]').click(function() {
        if ($('input[name=checkbox_1]').is(':checked')) {
            $('#checkbox_1').show('slow');
        }

        else {
            $('#checkbox_1').hide('slow');
        }
     });
     $('input[name=checkbox_2]').click(function() {
                   if ($('input[name=checkbox_2]').is(':checked')) {
            $('#checkbox_2').show('slow');
        }

        else {
            $('#checkbox_2').hide('slow');
        }
    });

  });

I have this need to show and hide a div if a checkbox is checked for multiple checkboxes.

Each div's id that needs to be shown will be the same as the checkboxes name. Is there an easy way to accomplish this without writing a function to handle each checkbox?

    $('input[name=checkbox_1]').click(function() {
        if ($('input[name=checkbox_1]').is(':checked')) {
            $('#checkbox_1').show('slow');
        }

        else {
            $('#checkbox_1').hide('slow');
        }
     });
     $('input[name=checkbox_2]').click(function() {
                   if ($('input[name=checkbox_2]').is(':checked')) {
            $('#checkbox_2').show('slow');
        }

        else {
            $('#checkbox_2').hide('slow');
        }
    });

  });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

全部不再 2024-09-16 00:39:05

您可以从名称中获取 ID,如下所示:

$('input[name^=checkbox_]').click(function() {
  $('#' + this.name)[this.checked ? 'show' : 'hide']('slow');
});

这使用 attribute-starts-使用选择器获取输入,然后使用其.name property 通过 ID 获取适当的元素。如果选中,则在具有该 ID 的元素上运行 .show('slow'),否则运行 .hide('slow')

You can get the ID from the name, like this:

$('input[name^=checkbox_]').click(function() {
  $('#' + this.name)[this.checked ? 'show' : 'hide']('slow');
});

This uses the attribute-starts-with selector to get the inputs, then uses their .name property to get the appropriate element by ID. If it's checked, runs .show('slow'), otherwise .hide('slow') on the element with that ID.

手心的温暖 2024-09-16 00:39:05
$('input[name^=checkbox]').click(function() {
    if(this.checked){
       $('#' + this.name).show('slow');
    } else{
       $('#' + this.name).hide('slow');
    }

}

编辑:使用“开始于”选择器会比检查indexOf更好,就像其他人所说的那样

$('input[name^=checkbox]').click(function() {
    if(this.checked){
       $('#' + this.name).show('slow');
    } else{
       $('#' + this.name).hide('slow');
    }

}

Edit: Using the 'starts with' selector would be better than checking the indexOf, like others said

囚你心 2024-09-16 00:39:05

看起来您正在进行某种类型的命名约定。您只需进入当前上下文对象,获取名称并按照约定进行操作即可。您可能想要为您的复选框字段提供一些特殊的 css 类,以便于轻松选择:

$('input.someCssClass').click(function() {

    var selector = '#' + $(this).attr('name');

    if ($(this).is(':checked'))
        $(selector).show('slow');
    else
        $(selector).hide('slow');
});

It looks like you have some type of naming convention going on. You can just reach into the current context object, get the name and work your convention. You might want to give your checkbox fields some special css class to make for easy selection:

$('input.someCssClass').click(function() {

    var selector = '#' + $(this).attr('name');

    if ($(this).is(':checked'))
        $(selector).show('slow');
    else
        $(selector).hide('slow');
});
非要怀念 2024-09-16 00:39:05

您可以在此处使用 toggle() 而不是 if ... else 来进一步减少代码:

('input[name^=checkbox_]').click(function(){
    $('#' + this.name).toggle('slow')
});

Toggle 还可以在交替点击时运行不同的功能,而不仅仅是隐藏/显示元素:

('input[name^=checkbox_]').toggle(function(){
    $('#' + this.name).hide('slow')
}, function(){
    $('#' + this.name).show('slow')
    } );

You can use toggle() instead of if ... else here to further reduce code:

('input[name^=checkbox_]').click(function(){
    $('#' + this.name).toggle('slow')
});

Toggle can also run different functions on alternate clicks instead of just hiding/showing elements:

('input[name^=checkbox_]').toggle(function(){
    $('#' + this.name).hide('slow')
}, function(){
    $('#' + this.name).show('slow')
    } );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文