Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
188 views
in Technique[技术] by (71.8m points)

javascript - 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:

<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:

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

Any help with this is very much appreciated!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
$(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. after getting the tr use .find() to search for the p.important and finally using .text() get the value.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...