简体   繁体   中英

Hide an element if div is empty for an html generated dynamically

Below is the html snippet:

<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. Below is the JS

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

  1. It says hide is not a function of args. I tried args.style.display = 'none' as well but that didn't work either.
  2. The div shown above shows up if a button is clicked. The args printed do not match what for the one I clicked (nothing prints for it). Any help would be greatly appreciated.

Why not just use Razor syntax instead of going through all of the trouble with 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.

 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> 

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