我有一个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 及其 child 。我尝试了这段代码
$('#attachmentDetail(' + index +') :input lstFiles['+index+'].IsDeleted').val("True");
但它说
jquery.min.js:2 Uncaught Error: Syntax error, unrecognized expression: #attachmentDetail(0)
有没有更好的方法来获取div的子元素?我想更新 IsDeleted
值,然后隐藏这个 div。
任何形式的帮助将不胜感激。
最佳答案
是的,有更好的方法来获取 child (以及 parent )。我会为所有可点击的<a>
设置一个独特的类。触发您的代码的标记,以便您可以按类分配事件处理程序。
$(".uniqueClass").click(function (e) {
var $div = $(this).parent();
var $deletedField = $div.find("input").first();
$deletedField.val(true);
});
现在您可以摆脱所有这些 ID 属性。
关于javascript - 更新 anchor 标记点击时的输入标记值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44057920/