2017-07-07 236 views
-1

設置。jquery選擇子元素

我正在使用MVC 5,並且我創建了一個視圖,其中以視圖模型的形式發送數據。

在此視圖中,我將List對象渲染爲堆疊的div,如下所示。如您所見,我顯示隱藏字段,以便viewModel在提交時將數據發送回控制器。

<div class="row item-row"> 
    <div class="small-4 columns"> 
     objType 
    </div> 
    <div class="small-6 columns"> 
     <input id="object_0__Id" name="object[0].Id" type="hidden" value="999999"> 
     <input id="object_0__Reference" name="object[0].Reference" type="hidden" value="myRef">myRef 
     <input id="object_0__RecordChanged" name="object[0].RecordChanged" type="hidden" value="NoChange"> 
    </div> 
    <div class="small-2 columns remove-item"> 
     <button class="button tiny expand centre button-gray" onclick="javascript: RemoveItem(999999);">Remove</button> 
    </div> 
</div> 
<div class="row item-row"> 
    <div class="small-4 columns"> 
     objType 
    </div> 
    <div class="small-6 columns"> 
     <input id="object_1__Id" name="object[1].Id" type="hidden" value="000001"> 
     <input id="object_1__Reference" name="object[1].Reference" type="hidden" value="myRef">myRef 
     <input id="object_1__RecordChanged" name="object[1].RecordChanged" type="hidden" value="NoChange"> 
    </div> 
    <div class="small-2 columns remove-item"> 
     <button class="button tiny expand centre button-gray" onclick="javascript: RemoveItem(000001);">Remove</button> 
    </div> 
</div> 

好了,所以javascript函數的removeItem是:

function RemoveItem(id) 
{ 
    event.preventDefault(); 

    var element = $(event.target).closest('.item-row'); 

    $(element).closest('DeedReference_0__RecordChanged').val('Deleted'); ***** This is what I am trying to do. 

    $(element).hide(); 
} 

根據以上所述,當我點擊發言權的removeItem(00001),可變元件包含以下內容:

<div class="small-4 columns"> 
    objType 
</div> 
<div class="small-6 columns"> 
    <input id="object_0__Id" name="object[0].Id" type="hidden" value="000001"> 
    <input id="object_0__Reference" name="object[0].Reference" type="hidden" value="myRef">myRef 
    <input id="object_0__RecordChanged" name="object[0].RecordChanged" type="hidden" value="NoChange"> 
</div> 
<div class="small-2 columns remove-item"> 
    <button class="button tiny expand centre button-gray" onclick="javascript: RemoveItem(000001);">Remove</button> 
</div> 

我需要更新的值是object [0] .RecordChanged,但在這個時候,我不知道索引值。所以我打算在選擇器上使用兩端,但是無法使其工作。

只要我有:

$(event.target).closest('.item-row').children()[1] 

但是這給了我在div,因爲我曾嘗試:

$(event.target).closest('.item-row').children()[1].Find('Id*"__RecordChanged"') 
$(event.target).closest('.item-row [id*="RecordChanged"]') 
$(event.target).closest('.item-row:[id*="RecordChanged"]) 

並採用可變

$(element [id*="RecordChanged"]) 
$(element [id$="RecordChanged"]) 

UPDATE

修復了代碼中的錯誤,提示我正在查看錯誤的索引。另外,如果我點擊RemoveItem(000001)的刪除按鈕,我試圖更新值object_0__RecordChanged。

+0

'最接近的'找到匹配給定選擇器的元素的最接近的祖先。你想要「孩子」還是「發現」? – evolutionxbox

+0

剛剛修復與代碼問題 – gilesrpa

回答

0

將視圖模型更改爲具有索引屬性。然後改變放置在一個foreach循環內的HTML.EditorFor使我能夠在它被渲染出來之前填充索引屬性。

function RemoveItem(id) 
{ 
    event.preventDefault(); 

    var element = $(event.target).closest('.item-row'); 

    $(element).closest('DeedReference_0__RecordChanged').val('Deleted'); ***** This is what I am trying to do. 

    $(element).hide(); 
} 

到:

然後將代碼從改變

function RemoveItem(id) 
{ 
    event.preventDefault(); 
    var recordChanged = '#object_' + id + '__RecordChanged'; 
    $(recordChanged).val('Deleted'); 
    var element = $(event.target).closest('.item-row'); 
    $(element).hide(); 
} 

簡單多了!