简体   繁体   English

HTML / jQuery最接近查找文本 <p> 标签

[英]HTML/jQuery finding text in closest <p> tag

I've got a table with different td's with the same ID, this is my table, in a foreach loop: 我在foreach循环中有一个表,它的td不同,ID相同,这是我的表:

<td class="AMLLeft" style="display:inline-block; !important">ID:
    <p class="important">${item.id}</p>
</td>
<td align="right" nowrap="true" class="AMLRight">Transaction sum: ${item.sum} ${item.currencyCode}</td>
</tr>
<tr class="hiddenRow">
    <td align="left">
        <div class="collapse123 demo${item.id}">
            <p>AML ID: ${item.id}</p>
        </div>
    </td>
    <td align="right">
        <div class="collapse123 demo${item.id}">
            <input type="button" value="Comment" class="btn btn-xs btn-primary commentButton" <a href="#myModal" data-toggle="modal" id="${item.id}" data-target="#edit-modal">
        </div>
    </td>
</tr>
<tr class="hiddenRow">
    <td align="left">
        <div class="collapse123 demo${item.id}">
            <p>AML Category: ${item.amlClientCategory}</p>
        </div>
    </td>
    <td align="right">
        <div class="collapse123 demo${item.id}">
            <input type="button" value="Close" class="btn btn-xs btn-primary">
        </div>
    </td>
</tr>
<tr class="hiddenRow">
    <td align="left">
        <div class="collapse123 demo${item.id}">
            <p>Client ID: ${item.clientId}</p>
        </div>
    </td>
    <td align="right">
        <div class="collapse123 demo${item.id}">
            <input type="button" value="Set Investigate" class="btn btn-xs btn-primary">
        </div>
    </td>
</tr>
<tr class="hiddenRow">
    <td align="left">
        <div class="collapse123 demo${item.id}">
            <p>Status: ${item.status}</p>
        </div>
    </td>
    <td align="right">
        <div class="collapse123 demo${item.id}"></div>
    </td>
</tr>
<tr class="hiddenRow">
    <td align="left">
        <div class="collapse123 demo${item.id}">
            <p>Comment: ${item.comment}</p>
        </div>
    </td>
    <td align="right">
        <div class="collapse123 demo${item.id}"></div>
    </td>
</tr>

I want to be able to find the value of the 2nd rows: <p class="important">${item.id}</p> , This is the code I've tried so far, which didn't work: 我希望能够找到第二行的值: <p class="important">${item.id}</p> ,这是我到目前为止尝试过的代码,但是没有用:

$(".commentButton").on('click', function () {
  var id = $(this).find('p.important').value();
  alert("id is:" + id);
});

Any help with this is very much appreciated! 非常感谢您的任何帮助!

$(this).closest('tr').siblings().find('p.important').text();

try this 尝试这个

Using the button select the tr use siblings() to get the tr of the p with .important. 使用按钮选择tr使用siblings()通过.important获取p的tr。 after getting the tr use .find() to search for the p.important and finally using .text() get the value. 获取tr后,使用.find()搜索p.important ,最后使用.text()获得值。

$(".commentButton").on('click', function () {
  var id = $(document).find('p.important').html();
  alert("id is:" + id);
});

this is your button, which does not contain the item your searching. this是您的按钮,其中不包含您要搜索的项目。
.html gets the value between the html tags .html获取html标记之间的值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM