簡體   English   中英

在錨標簽單擊時更新輸入標簽值

[英]Update input tag value on anchor tag click

我有一個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++;
 }

我想更新@Model.lstFiles[i].IsDeleted價值btnDelFile_Click 但是我無法得到div及其孩子。 我嘗試了這段代碼

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

但它說

jquery.min.js:2未捕獲的錯誤:語法錯誤,無法識別的表達式:#attachmentDetail(0)

有沒有更好的辦法讓div的孩子? 我想更新IsDeleted值,然后隱藏此div。

任何幫助將不勝感激。

您可以為您的元素設置一個id,並使用document.getElementById來獲取它,然后相應地對其進行更新。

是的,有一種更好的方法來獲得孩子(還有父母)。 我會在所有可觸發的<a>標記上設置一個唯一的類,這些類會觸發您的代碼,以便您可以按類分配事件處理程序。

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

現在,您可以擺脫所有這些ID屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM