我有这个html代码。我想通过输入值得到id值。
例如:我想知道id值,它的值=number2=>这是未知的2。我如何使用jQuery做到这一点?

<th id="unknown1" rel="0">
<input id="btnSendMessage" type="button" value=number1 class="red"/>
</th>

<th id="unknown2" rel="0">
<input id="btnSendMessage" type="button" value=number2 class="red"/>
</th>

<th id="unknown3" rel="0">
<input id="btnSendMessage" type="button" value=number3 class="red"/>
</th>

最佳答案

你可以这样做:-

$("input[value='number2']").parent().attr('id')

示例:-
$(document).ready(function(){
  console.log($("input[value='number2']").parent().attr('id'));
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <th id="unknown1" rel="0">
      <input id="btnSendMessage" type="button" value=number1 class="red"/>
    </th>

    <th id="unknown2" rel="0">
      <input id="btnSendMessage" type="button" value=number2 class="red"/>
    </th>

    <th id="unknown3" rel="0">
      <input id="btnSendMessage" type="button" value=number3 class="red"/>
    </th>
  </tr>
</table>

注:-
没有<th><table></table>将不会在浏览器上呈现,因此它将undefined。如果你想让它工作,那么<th>必须在<table></teable>里面。

10-07 13:46
查看更多