简体   繁体   English

隐藏的元素,如果DIV是空的用于动态地生成一个HTML

[英]Hide an element if div is empty for an html generated dynamically

Below is the html snippet: 以下是html代码段:

<div class="row activityrow">
   @using (Html.BeginForm("DownloadFile", "GetData", FormMethod.Post, new {        target = "_blank" })){
      <label id='label_@data.test.Replace(".",@function_name).Replace("          ","")'><input type="checkbox" class="selectall" /> Select all</label>
      <div class="containsdata" style="column-count:2;width:100%;">
        @{ var count = 0;}
        @foreach (var item in data.Documents)
        {
           @if (@item.testdata.Replace(" ", "").Replace("-", "_").Replace("&","_").Replace("?", "") == function_value.Name)
           {
             <div class="right8">
               <label>
                  <input type="checkbox" id="chk_@item.ID" value="@item.ID" name="chkId" />
                  <button type="submit" class="btn btn-link" id="@item.ID"          value="@item.ID" name="Id"> @item.Title.Replace("Deploy ","")</button>
                </label>
            </div>
            count++;
        }
    }
 </div>
  @if (count > 0)
  {
    <button type="submit" class="btn btn-primary" id="selectedDownload" name="submit"> Download Selected</button>
  }
  else
  {
     <script>hideSelect('label_@data.test.Replace(".",          
       @function_name).Replace(" ", "")');
     </script>
 }

in the above I want to display the label for select all ONLY if the count > 0. I tried calling the hideSelect function to do that but it doesn't work. 在上面的示例中,我想仅在count> 0时才显示全选标签。我尝试调用hideSelect函数来执行此操作,但它不起作用。 Below is the JS 下面是JS

<script>
  function hideSelect(args) {
  args.hide();
  console.log(args);
}

  1. It says hide is not a function of args. 它说hide不是args的函数。 I tried args.style.display = 'none' as well but that didn't work either. 我试图args.style.display =“无”,以及但是这也不能工作。
  2. The div shown above shows up if a button is clicked. 如果单击按钮,则会显示上面显示的div。 The args printed do not match what for the one I clicked (nothing prints for it). 打印的ARGS不匹配的内容的一个,我点击(它没有打印)。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Why not just use Razor syntax instead of going through all of the trouble with javascript? 为什么不只使用Razor语法而不使用javascript来解决所有麻烦?

if(data.Documents.Count > 0){
    <label id='label_@data.test.Replace(".",@function_name).Replace(" ","")'><input type="checkbox" class="selectall" /> Select all</label>
}

or 要么

<label id='label_@data.test.Replace(".",@function_name).Replace(" ","")' @if(data.Documents.Count == 0){<text>style='display:none;'</text>}><input type="checkbox" class="selectall" /> Select all</label>

I think your only issue is that you are attempting to hide a string. 我认为你唯一的问题是,你试图隐藏的字符串。 Let's turn that string into a selector and use it to target a specific element using jQuery. 让我们将该字符串转换为选择器,并使用它使用jQuery定位特定元素。

 function hideSelect(args) { $(`#${args}`).hide(); console.log(args); } hideSelect("label_something_something"); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="label_something_something"> Can you see me? </div> 

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

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