DOM表获取rel属性

时间:2013-01-21 20:15:08

标签: jquery dom

我试图获取表格中已检查单选按钮的兄弟的rel属性:

<table id="table-handing-subjects-content" class="table-content-all">
  <tbody>
    <tr for="handling-subject0">
      <td style="width:10%;">
        <input id="handling-subject[0]" type="radio" value="2" name="handling_subject">
      </td>
      <td style="width:25%;">
        <label for="handling-subject[0]"> economics</label>
      </td>
      <td style="width:15%;">
           <label for="handling-subject[0]"> firstyear</label>
      </td>
      <td style="width:20%;">
           <label id="ref-sec-id[0]" for="handling-subject[0]" rel="36"> black</label>
      </td>
      <td style="width:30%;">
         <label for="handling-subject[0]"> asdf,asdf asdf</label>
      </td>
 </tr>
 <tr for="handling-subject1">
   <td style="width:10%;">
      <input id="handling-subject[1]" type="radio" value="3" name="handling_subject">
   </td>
   <td style="width:25%;">
      <label for="handling-subject[1]"> math</label>
   </td>
   <td style="width:15%;">
      <label for="handling-subject[1]"> firstyear</label>
   </td>
   <td style="width:20%;">
      <label id="ref-sec-id[1]" for="handling-subject[1]" rel="36"> black</label>
   </td>
   <td style="width:30%;">
       <label for="handling-subject[1]"> asdf,asdf asdf</label>
   </td>
</tr>
<tr for="handling-subject2">
   <td style="width:10%;">
        <input id="handling-subject[2]" type="radio" value="3" name="handling_subject">
   </td>
   <td style="width:25%;">
        <label for="handling-subject[2]"> math</label>
   </td>
   <td style="width:15%;">
        <label for="handling-subject[2]"> firstyear</label>
   </td>
   <td style="width:20%;">
        <label id="ref-sec-id[2]" for="handling-subject[2]" rel="32"> level-up</label>
   </td>
   <td style="width:30%;">
         <label for="handling-subject[2]"> asdf,asdf asdf</label>
    </td>
   </tr>
  </tbody>
 </table>

我试图通过此

获取已检查的radiobutton的id =“ref-sec-id”的标签
   $('input[name=handling_subject]:checked').parent().parent().parent().children().find("td:eq(3)").children().attr("rel");

但我想我只是在第一排用未经检查的单选按钮。请帮助......并提前感谢

1 个答案:

答案 0 :(得分:1)

$("input[name=handling_subject]:checked").closest('tr').find('[rel]').attr('rel')

您的HTML会导致输入相对于表格的父项数量不一致。

您还应该使标签上的所有id属性都是唯一的,但这不会对您使用上述选择器的能力产生任何影响。