2017-05-18 197 views
2

输入标签值我有一个div更新锚标签点击

@{int index = 0;} 
@for (int i = 0; i < Model.lstFiles.Count; i++) 
{ 
    <div id="attachmentDetail(@index)" name="attachmentDetail(@index)" class="panel-body"> 
     <a asp-action="Download" asp-controller="Admin" asp-route-realName=Model.lstFiles[i].RealName>@Model.lstFiles[i].FileName</a> 
     <a onclick="btnDelFile_Click(@index)" id="btnDelFile{@index}" class="pull-right" title="delete"><img height="20" width="20" src="@Url.Content("~/images/delete.jpg")" /></a> 
     <input type="text" asp-for="@Model.lstFiles[i].IsDeleted" hidden /> 
     <input type="text" asp-for="@Model.lstFiles[i].FileId" hidden /> 
     <hr /> 
    </div> 
    index++; 
} 

我要更新btnDelFile_Click@Model.lstFiles[i].IsDeleted值。但我无法获得div和它的孩子。我想这个代码

$('#attachmentDetail(' + index +') :input lstFiles['+index+'].IsDeleted').val("True"); 

但它说

jquery.min.js:2 Uncaught Error: Syntax error, unrecognized expression: #attachmentDetail(0)

有没有更好的办法让DIV的孩子?我想更新IsDeleted的值,然后隐藏这个div。

任何形式的帮助将不胜感激。

回答

1

是的,有一个更好的方式来获得孩子(也是父母)。我会在所有可触击的<a>标签上设置一个独特的类,它会触发您的代码,以便您可以按类分配事件处理程序。

$(".uniqueClass").click(function (e) { 
    var $div = $(this).parent(); 
    var $deletedField = $div.find("input").first(); 
    $deletedField.val(true); 
}); 

现在你可以摆脱所有这些ID属性。

+0

工作就像一个魅力。谢谢 :) –

1

您可以为您的元素设置一个id,并使用document.getElementById来获取它,然后相应地更新它。

+0

但我不知道哪个元素将被点击,所以我需要一个合适的方式来获得该元素。 –