jQuery在每个单选按钮或复选框旁边添加空标签标签

hen*_*ron 1 jquery input add next radio-button

我需要一个简单的jQuery代码,它会在每个单选按钮或复选框旁边添加一个空标签

例如:

$('input[type="radio"]').next().add("<label for="radio"></label>");
$('input[type="checkbox"]').next().add("<label for="checkbox"></label>");
Run Code Online (Sandbox Code Playgroud)

我怎么能做到这一点?

谢谢.

Ada*_*kis 5

你会想用后做到这一点.此外,您还希望for您的标签是id它所指的标签.

$('input[type="checkbox"]').each(function() {
     var cb = $(this);
     cb.after($("<label />")
                .attr("for", cb.attr("id")) );
});
Run Code Online (Sandbox Code Playgroud)

你说你想要空标签,但为了以防万一,如果你想让标签显示他们旁边的复选框的值,这应该是这样的:

$('input[type="checkbox"]').each(function() {
     var cb = $(this);
     cb.after($("<label />")
                .attr("for", cb.attr("id"))
                .text(cb.val()) );
});
Run Code Online (Sandbox Code Playgroud)