指向特定的行jquery

时间:2018-02-05 19:39:33

标签: jquery

我已经编写了一个代码片段并且它正常工作但我想添加其他功能,但我不知道应该如何添加它。任何建议都会受到欢迎。代码片段的作用是遍历每一行并找到一个特定的单元格并对其进行一些处理。我想要做的是获得每个输入最接近的标签,但我得到null。我删除了复杂的命名等,以使代码段尽可能简单,因为我想在这里获得功能。输入和标签都在同一个单元格(td)中。 here is the structure of the html cells and row

var count = 0;
$("tr").each(function () {
    var td = $(this).find('td');
    $(td).each(function () {
        var input = $(this).find('input');
        $(input).each(function () {
            count = count +1;
            $(this).closest('label').attr('for', count); //I have alerted this label's text but I got null meaning it isn't getting anything
        });
    });
});

1 个答案:

答案 0 :(得分:0)

不要使用jQuery做一些时髦的事情,而是编辑你的HTML并将input打包成label

<td>
    <label>
        <input type="radio" name="radio_input_2" value="radio 1">
        radio 1
    </label>
</td>
完成工作。没有一行JS。
并且不需要idfor属性。

相关问题