使用 jQuery 迭代类似命名的复选框
我正在尝试使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定我 100% 理解你的问题,但我对以下选择器风格非常怀疑:
我猜你想知道“checked”属性的值,这更像是......
I'm not sure I understand your question 100%, but I'm very suspicious of the following style of selector:
I'm guessing you want to know the value of the 'checked' attribute, which would be more like...