下面是 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>
}
在上面,我想仅在计数 > 0 时显示全选标签。我尝试调用 hideSelect 函数来执行此操作,但它不起作用。下面是JS
<script>
function hideSelect(args) {
args.hide();
console.log(args);
}
- 它说 hide 不是 args 的函数。我也尝试过 args.style.display = 'none' 但也不起作用。
- 如果单击按钮,则会显示上面显示的 div。打印的参数与我单击的参数不匹配(没有打印任何内容)。任何帮助将不胜感激。
最佳答案
为什么不直接使用 Razor 语法而不是使用 javascript 解决所有麻烦?
if(data.Documents.Count > 0){
<label id='label_@data.test.Replace(".",@function_name).Replace(" ","")'><input type="checkbox" class="selectall" /> Select all</label>
}
或
<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>
关于javascript - 如果动态生成的 html 的 div 为空,则隐藏元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52823695/