以下是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>
}
在上面的中,我想仅在count> 0时才显示全选标签。我尝试调用hideSelect函数来执行此操作,但它不起作用。下面是JS
<script>
function hideSelect(args) {
args.hide();
console.log(args);
}
答案 0 :(得分:2)
为什么不仅仅使用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>
答案 1 :(得分:0)
我认为您唯一的问题是您尝试隐藏字符串。让我们将该字符串转换为选择器,并使用它使用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>