简体   繁体   English

使用Javascript将按钮隐藏在表格的最后一行

[英]Hide the button in the last row of the table using Javascript

Here's my html 这是我的HTML

  <table class="table table-responsive table-bordered" style="width:100%" id="HeightConfig">
    <tbody id="HeightConfigBody">
@if (Model != null)
{
@foreach (var data in Model.Reverse())
{
     <tr>
                         <td style="width:40%">
  <label><b>Recorded Time:</b> <input type="text" id="recordDate"  class="recordDate"   
  value="@data.recordDate.ToString("yyyy-MM-dd")" disabled></label>
                      </td>



                       <td style="width:40%">
                         <label><b>Height (CM):</b> <input type="text" id="ColumnTypeData" class="ColumnTypeData" 
                          value="@data.ColumnTypeData" ></label>
                      </td>

                      <td style="width:40%">
                           <a class="btn btn-primary btn-default" title="delete"
                            data-target="#modal-container" data-toggle="modal" data-align="center">
                            <div id="minusHeight"><i class="fa fa-minus" aria-hidden="true"></i></div>
                            </a>


     <a class="btn btn-primary btn-default" title="add"  >
                              <div id="addHeight" ><i class="fa fa-plus" aria-hidden="true"></i></div>
     </a>
                       </td>

</tr>

}
}

     </tbody>
  </table>

I need to hide the "plus button" in the third column, and display only in the last row. 我需要在第三列中隐藏“加号按钮”,并仅在最后一行中显示。

this is what I got 这就是我得到的

<script>
 debugger;
        var x = document.getElementById("HeightConfig").rows.length;
        var ctr = 1
        $("#HeightConfig tbody tr").each(function (key, value) {
            debugger;

            if (ctr != x)
            {
                $(this).find('.addHeight').hide();

            }
            ctr++;
        });
</script>

i get first the length of the table, then loop on each row, then when the counter match the last row of the table it will not hide the button 我首先得到表的长度,然后在每一行上循环,然后当计数器与表的最后一行匹配时,它不会隐藏按钮

No need for all that (as you guessed). 不需要所有这些(您猜到了)。 :-) :-)

In modern browsers (basically anything except IE8 and earlier; details ), you could do it with CSS via :not with :last-of-type : 在现代浏览器中(基本上是IE8和更早版本; 细节 ),您可以通过:not:last-of-type一起使用CSS来:last-of-type

#HeightConfig tr:not(:last-of-type) a[title=add] {
  display: none;
}

Example: 例:

 #HeightConfig tr:not(:last-of-type) a[title=add] { display: none; } 
 <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/> <table class="table table-responsive table-bordered" style="width:100%" id="HeightConfig"> <tbody id="HeightConfigBody"> <tr> <td style="width:40%"> <label><b>Recorded Time:</b> <input type="text" id="recordDate" class="recordDate" value="@data.recordDate.ToString("yyyy-MM-dd")" disabled></label> </td> <td style="width:40%"> <label><b>Height (CM):</b> <input type="text" id="ColumnTypeData" class="ColumnTypeData" value="@data.ColumnTypeData" ></label> </td> <td style="width:40%"> <a class="btn btn-primary btn-default" title="delete" data-target="#modal-container" data-toggle="modal" data-align="center"> <div id="minusHeight"><i class="fa fa-minus" aria-hidden="true"></i></div> </a> <a class="btn btn-primary btn-default" title="add"> <div id="addHeight"><i class="fa fa-plus" aria-hidden="true"></i></div> </a> </td> </tr> <tr> <td style="width:40%"> <label><b>Recorded Time:</b> <input type="text" id="recordDate" class="recordDate" value="@data.recordDate.ToString("yyyy-MM-dd")" disabled></label> </td> <td style="width:40%"> <label><b>Height (CM):</b> <input type="text" id="ColumnTypeData" class="ColumnTypeData" value="@data.ColumnTypeData" ></label> </td> <td style="width:40%"> <a class="btn btn-primary btn-default" title="delete" data-target="#modal-container" data-toggle="modal" data-align="center"> <div id="minusHeight"><i class="fa fa-minus" aria-hidden="true"></i></div> </a> <a class="btn btn-primary btn-default" title="add"> <div id="addHeight"><i class="fa fa-plus" aria-hidden="true"></i></div> </a> </td> </tr> <tr> <td style="width:40%"> <label><b>Recorded Time:</b> <input type="text" id="recordDate" class="recordDate" value="@data.recordDate.ToString("yyyy-MM-dd")" disabled></label> </td> <td style="width:40%"> <label><b>Height (CM):</b> <input type="text" id="ColumnTypeData" class="ColumnTypeData" value="@data.ColumnTypeData" ></label> </td> <td style="width:40%"> <a class="btn btn-primary btn-default" title="delete" data-target="#modal-container" data-toggle="modal" data-align="center"> <div id="minusHeight"><i class="fa fa-minus" aria-hidden="true"></i></div> </a> <a class="btn btn-primary btn-default" title="add"> <div id="addHeight"><i class="fa fa-plus" aria-hidden="true"></i></div> </a> </td> </tr> <tr> <td style="width:40%"> <label><b>Recorded Time:</b> <input type="text" id="recordDate" class="recordDate" value="@data.recordDate.ToString("yyyy-MM-dd")" disabled></label> </td> <td style="width:40%"> <label><b>Height (CM):</b> <input type="text" id="ColumnTypeData" class="ColumnTypeData" value="@data.ColumnTypeData" ></label> </td> <td style="width:40%"> <a class="btn btn-primary btn-default" title="delete" data-target="#modal-container" data-toggle="modal" data-align="center"> <div id="minusHeight"><i class="fa fa-minus" aria-hidden="true"></i></div> </a> <a class="btn btn-primary btn-default" title="add"> <div id="addHeight"><i class="fa fa-plus" aria-hidden="true"></i></div> </a> </td> </tr> </tbody> </table> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

Otherwise, with jQuery, you can use :not and the jQuery-specific :last : 否则,对于jQuery,您可以使用:not和特定于jQuery的:last

$("#HeightConfig tr:not(:last) a[title=add]").hide();

Example: 例:

 $("#HeightConfig tr:not(:last) a[title=add]").hide(); 
 <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/> <table class="table table-responsive table-bordered" style="width:100%" id="HeightConfig"> <tbody id="HeightConfigBody"> <tr> <td style="width:40%"> <label><b>Recorded Time:</b> <input type="text" id="recordDate" class="recordDate" value="@data.recordDate.ToString("yyyy-MM-dd")" disabled></label> </td> <td style="width:40%"> <label><b>Height (CM):</b> <input type="text" id="ColumnTypeData" class="ColumnTypeData" value="@data.ColumnTypeData" ></label> </td> <td style="width:40%"> <a class="btn btn-primary btn-default" title="delete" data-target="#modal-container" data-toggle="modal" data-align="center"> <div id="minusHeight"><i class="fa fa-minus" aria-hidden="true"></i></div> </a> <a class="btn btn-primary btn-default" title="add"> <div id="addHeight"><i class="fa fa-plus" aria-hidden="true"></i></div> </a> </td> </tr> <tr> <td style="width:40%"> <label><b>Recorded Time:</b> <input type="text" id="recordDate" class="recordDate" value="@data.recordDate.ToString("yyyy-MM-dd")" disabled></label> </td> <td style="width:40%"> <label><b>Height (CM):</b> <input type="text" id="ColumnTypeData" class="ColumnTypeData" value="@data.ColumnTypeData" ></label> </td> <td style="width:40%"> <a class="btn btn-primary btn-default" title="delete" data-target="#modal-container" data-toggle="modal" data-align="center"> <div id="minusHeight"><i class="fa fa-minus" aria-hidden="true"></i></div> </a> <a class="btn btn-primary btn-default" title="add"> <div id="addHeight"><i class="fa fa-plus" aria-hidden="true"></i></div> </a> </td> </tr> <tr> <td style="width:40%"> <label><b>Recorded Time:</b> <input type="text" id="recordDate" class="recordDate" value="@data.recordDate.ToString("yyyy-MM-dd")" disabled></label> </td> <td style="width:40%"> <label><b>Height (CM):</b> <input type="text" id="ColumnTypeData" class="ColumnTypeData" value="@data.ColumnTypeData" ></label> </td> <td style="width:40%"> <a class="btn btn-primary btn-default" title="delete" data-target="#modal-container" data-toggle="modal" data-align="center"> <div id="minusHeight"><i class="fa fa-minus" aria-hidden="true"></i></div> </a> <a class="btn btn-primary btn-default" title="add"> <div id="addHeight"><i class="fa fa-plus" aria-hidden="true"></i></div> </a> </td> </tr> <tr> <td style="width:40%"> <label><b>Recorded Time:</b> <input type="text" id="recordDate" class="recordDate" value="@data.recordDate.ToString("yyyy-MM-dd")" disabled></label> </td> <td style="width:40%"> <label><b>Height (CM):</b> <input type="text" id="ColumnTypeData" class="ColumnTypeData" value="@data.ColumnTypeData" ></label> </td> <td style="width:40%"> <a class="btn btn-primary btn-default" title="delete" data-target="#modal-container" data-toggle="modal" data-align="center"> <div id="minusHeight"><i class="fa fa-minus" aria-hidden="true"></i></div> </a> <a class="btn btn-primary btn-default" title="add"> <div id="addHeight"><i class="fa fa-plus" aria-hidden="true"></i></div> </a> </td> </tr> </tbody> </table> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

You can solve this using simple CSS 您可以使用简单的CSS解决此问题

#HeightConfig tr a[title=add] {
  display: none;
}

#HeightConfig tr:last-child a[title=add] {
  display: block;
}

or using jQuery 或使用jQuery

$("#HeightConfig tr a[title=add]").hide();
$("#HeightConfig tr:last-child a[title=add]").show();

As per your comment, If you want just hide all buttons you can simply put: 根据您的评论,如果您只想隐藏所有按钮,则可以简单地输入:

$("#HeightConfig tr a[title=add]").hide();

or in css 或在CSS

#HeightConfig tr a[title=add] {
   display: none;
}

addHeight is the id of the div and not the class name. addHeight是div的ID ,而不是类名。 You can either make it the class name or use 您可以将其设为类名或使用

 $(this).find('#addHeight').hide();

in the if condition of your code. 在代码的if条件中。

If you want to hide all the buttons simply make addheight the class name of the div and then use the following line: 如果要隐藏所有按钮,只需将addheight设置为div的类名称,然后使用以下行:

$('.addHeight').hide();

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

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