简体   繁体   English

如何在行表中查找div元素并返回值?

[英]How to find a div element inside the row table and return the value?

I have a table below which row is being looped by foreach statement. 我有一张表,下面的行被foreach语句循环。 Inside the table, there are various div elements as you can see. 在表格内,您可以看到各种div元素。 I want to get and return the value of one of the div element with a class="divrep" by clicking the button with a class ="postrep" . 我想,并通过点击一类=“postrep”按钮返回一类=“divrep”的div元素的一个值。 I tried something below but it returns null object so I'm not sure if I get the right div element. 我在下面尝试了一些方法,但是它返回空对象,所以我不确定是否得到正确的div元素。

jQuery: jQuery的:

$(function () {
           $('.postrep').click(function () {
               var findiv = $(this).closest('table').find('div[class="divrep"]');
               alert(findiv.toString());

            });
        });

HTML: HTML:

<table id="mytable">  

    <tr >
        <td class="tdstyle" >

  <div style="font-weight:bold;">  @Html.DisplayFor(modelItem => item.name) </div> 

   <p >  @Html.DisplayFor(modelItem => item.comment) </p>
  <p> <input type="button" id="like" name="like" value="Like" style="color:blue;border:0px;background-color:inherit;cursor:pointer" /> <input type="button" class ="Reply" name="Reply" value="Replie(s)" /></p>                                                                                                                                 
  <div id="divReply" class ="divrep"> I want to return this string value...

     <div> 
        <div class="editor-field" style="display:none; margin-bottom:5px;margin-top:5px">  
          <input type="text" id="comidvalue" name="id" class="id" value="@Html.DisplayFor(modelItem => item.Id)" />
        </div>

        <br />
        <input type="text" id="namerep" name="name" class="name" style="width:445px;resize:none" />

       <br />
       <textarea id="reply" name="reply" class="reply" style="width:445px;height:100px;resize:none" ></textarea>

         <br />

        <input type="button" class="postrep" value="Post Reply" name="butname" style="cursor:pointer" /> 
      </div>
            <br />

  </div>

        </td>       
    </tr>

</table>

Try to use .contents() to grab the text nodes as well as the child elements and get the first element from the returned collecion, 尝试使用.contents()来获取文本节点以及子元素,并从返回的集合中获取第一个元素,

$('.postrep').click(function () {
    var findiv = $(this).closest('.divrep');
    alert(findiv.contents()[0].nodeValue);
});

Using .text() over the finddiv object would be recommended, but if suppose you have more text nodes inside of finddiv then prefer this solution. 建议在finddiv对象上使用.text() ,但是如果假定在finddiv有更多文本节点,则首选此解决方案。

DEMO 演示

What about this way? 那这样呢

        $(function () {
           $('.postrep').click(function () {
               var findiv = $(this).closest('.divrep');
               alert(findiv.html());

            });
        });

try the fiddle just change the api as 尝试小提琴只是将api更改为

alert(findiv.text());

hope it helps 希望能帮助到你

for select the dive with class divrep the selector is ".divrep" and in addintion per return the content of the node you must use the api html if you want inner html or the api text if you wont the content as simply text 对于选择带有divrep类的潜水,选择器为“ .divrep”,并且在每个返回的内容中,如果要使用内部html,则必须使用api html;如果您不想将内容作为简单文本,则必须使用api 文本

var findiv = $(this).closest('table').find('.divrep');
 alert(findiv.html());

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

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