当有多个时,jQuery从一个类中获取子进程

时间:2016-12-06 15:43:23

标签: javascript jquery

我有以下HTML:

<div class="thread_pane">
    <div class="thread_message">
        <table>
            <tr>
                <th class="thread_image">
                    <img src="image" class="actual_image">
                </th>
                <th class="thread_preview">
                    <small class="thread_sender">Father Ted</small><br/>
                    <small class="thread_preview_text">Father: test...</small>
                    <input class="session_id" type="hidden" value="b3a4c17ac149860e9f42624656cdb53c"/>
                </th>
            </tr>
         </table>
     </div>
     <div class="thread_message">
         <table>
             <tr>
                 <th class="thread_image">
                     <img src="image" class="actual_image">
                 </th>
                 <th class="thread_preview">
                     <small class="thread_sender">Chuck Norris</small><br/>
                     <small class="thread_preview_text">Chuck: another test message...</small>
                     <input class="session_id" type="hidden" value="e3ed07c78d16e62df4d4f7efd33a02f4"/>
                  </th>
              </tr>
          </table>
      </div>
  </div>

这是从PHP生成的,可能有无限量,我有以下jQuery来与元素交互:

$(".thread_message").click(function () {
    var session_id = $('.thread_message').find(".session_id").val();
    alert("loading message thread:-" + session_id + "-");
});

我试图通过此方式提醒您点击的div中显示的特定session_id,而不仅仅是发现当前正在发生的第一个。

1 个答案:

答案 0 :(得分:2)

您需要使用$(this)代替$('.thread_message')

&#13;
&#13;
$(".thread_message").click(function () {
    var session_id = $(this).find(".session_id").val();
    alert("loading message thread:-" + session_id + "-");
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="thread_pane">
    <div class="thread_message">
        <table>
            <tr>
                <th class="thread_image">
                    <img src="image" class="actual_image">
                </th>
                <th class="thread_preview">
                    <small class="thread_sender">Father Ted</small><br/>
                    <small class="thread_preview_text">Father: test...</small>
                    <input class="session_id" type="hidden" value="b3a4c17ac149860e9f42624656cdb53c"/>
                </th>
            </tr>
         </table>
     </div>
     <div class="thread_message">
         <table>
             <tr>
                 <th class="thread_image">
                     <img src="image" class="actual_image">
                 </th>
                 <th class="thread_preview">
                     <small class="thread_sender">Chuck Norris</small><br/>
                     <small class="thread_preview_text">Chuck: another test message...</small>
                     <input class="session_id" type="hidden" value="e3ed07c78d16e62df4d4f7efd33a02f4"/>
                  </th>
              </tr>
          </table>
      </div>
  </div>
&#13;
&#13;
&#13;