使用 jQuery 迭代类似命名的复选框

发布于 2024-08-19 05:55:55 字数 866 浏览 6 评论 0原文

我正在尝试使用 jQueryeach() 迭代一组名为 seller_type[] 的复选框。它们都有唯一的 id,所以我可以重写它来处理一系列唯一的 id,但我想找出我在这里出错的地方。

该警报显示,虽然 intIndex 看到了正确的元素数量,但 this 始终保存第一个元素。

function setUp(){
    $( 'input[name=supplier_type[]]' ).each(
        function( intIndex ){
            var type = $(this+':checked').val() + 'Table';
            $("#"+type).show("slow");
            alert($(this+':checked').val());  // sees first one each iter
        }       
    );
}

那么我该如何处理正确的元素呢?我需要使用bind()吗?

谢谢!

// 编辑 基于德鲁·威尔斯回应的工作版本。谢谢德鲁!

function setUp(){
    $( 'input[name=supplier_type[]]' ).each(
        function( intIndex ){
            if($(this).attr('checked')) {
                var type = $(this).val() + 'Table';
                $("#"+type).show("slow");
            }
        }       
    );
}

I am trying to use jQuery each() to iterate across a group of checkboxes named supplier_type[]. They all have unique ids so I could just re-write this to handle an array of unique ids but I wanted to figure out where I went wrong here.

The alert shows that while intIndex sees the correct number of elements this always holds the first element.

function setUp(){
    $( 'input[name=supplier_type[]]' ).each(
        function( intIndex ){
            var type = $(this+':checked').val() + 'Table';
            $("#"+type).show("slow");
            alert($(this+':checked').val());  // sees first one each iter
        }       
    );
}

So how do I address the correct element? Do I need to use bind()?

Thanks!

// Edit
Working version based on Drew Wills response. Thanks Drew!

function setUp(){
    $( 'input[name=supplier_type[]]' ).each(
        function( intIndex ){
            if($(this).attr('checked')) {
                var type = $(this).val() + 'Table';
                $("#"+type).show("slow");
            }
        }       
    );
}

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

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

发布评论

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

评论(1

梦屿孤独相伴 2024-08-26 05:55:55

我不确定我 100% 理解你的问题,但我对以下选择器风格非常怀疑:

$(this+':checked')

我猜你想知道“checked”属性的值,这更像是......

$(this).attr('checked')

I'm not sure I understand your question 100%, but I'm very suspicious of the following style of selector:

$(this+':checked')

I'm guessing you want to know the value of the 'checked' attribute, which would be more like...

$(this).attr('checked')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文