2017-05-18 199 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

但我不知道哪個元素將被點擊,所以我需要一個合適的方式來獲得該元素。 –