简体   繁体   中英

Hide and Show table inside a another table row

I have table that is generating dynamically:

tables.ejs

 <table>
    <% for(var d=0;d<docs.length;d++)
      {  %>
        <tr>
        <td>
            <img src="http://www.bls.gov/images/icons/icon_small_plus.gif" class="image1" 

              id="image1" onclick=diffImage(this) />
        </td>
        <td>
          <p><%= docs[d].name %></p></a>
        </td>
        <td>
          <%= docs[d].url_Address %>
        </td>
        <td>
          <%= docs[d].product_Name %></p></a>
        </td>
        </tr>
    <div id= "sub" class="sub">
        <tr class="sub" id="<%= docs[d]._id %>" >
            <table style="width:100%;font-size: 14px;" align="right" bgcolor="#A0A0A3" >
            <% for(var e=0;e<doc.length;e++)
              {   %>
                 <tr>
                    <td> 
                       <a href=<%= doc[e].url_Address %>><p><%= doc[e].product_Name %></p></a>
                    </td>
                    <td>
                    <p class = "tab"><%= doc[e].vendor_Name %></p>
                    </td>
                 </tr>
          <% } %>
           </table>
        </tr>
    </div>
    <% } %>
    </table>

now I want when I click on image then the image will change and the div should hide

I try following jquery .

<script>
  function diffImage(img) {
   if(img.src.match("http://olenka.med.virginia")){
        img.src = "http://www.bls.gov/images/icons/icon_small_plus.gif";
        $(".image1").click(function(){
            $(".sub").hide();
         });
   }else{
        $(".image1").click(function(){
            $(".sub").show();
        });
        img.src = "http://olenka.med.virginia.edu/psi/images/icons/minus_icon.png";
        }
    }
</script>

its hiding the div on click on image .

but the problem is :

when i click to image for showing sub table it is showing all the sub table of every row , and same when trying to hide row ?

I want when click on image then only its sub table is hide or show not all sub table ?

Try this:

<script>
  function diffImage(img) {
   if(img.src.match("http://olenka.med.virginia")){
        img.src = "http://www.bls.gov/images/icons/icon_small_plus.gif";
        $(".image1").click(function(){
            $(this).closest('tr').next('.sub').hide();
         });
   }else{
        $(".image1").click(function(){
            $(this).closest('tr').next('.sub').show();
        });
        img.src = "http://olenka.med.virginia.edu/psi/images/icons/minus_icon.png";
        }
    }
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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