如何获取属性的复选框值

Man*_*ati 4 jquery

我想获得复选框的属性值的值.我的代码给了我undefined

$('#mytable').find('tr').each(function () {
            var row = $(this);

            if ( row.find('input[type="checkbox"]').is(':checked') ) {
                alert( $(this).attr("b_partner_id") );
            }
        });
Run Code Online (Sandbox Code Playgroud)

Aru*_*hny 5

this是指块中的tr元素if,因为您获取b_partner_idtr元素中不存在您的属性undefined.

相反,您需要使用checkbox引用获取属性值

$('#mytable').find('tr').each(function () {
    var row = $(this),
        $check = row.find('input[type="checkbox"]');

    if ($check.is(':checked')) {
        alert($check.attr("b_partner_id"));
    }
});
Run Code Online (Sandbox Code Playgroud)