2013-01-17 83 views
0

好的,所以我有一個包含單個行或父行的表以及與該父級鏈接的幾個子級。我有圖標可以刪除父親或個人的兒子。圖標將顯示模式彈出(而不是警報)以確認刪除。它有效,但我將不得不重複一遍。我想這可以簡化以找到一個ID或類,因爲模式顯示/隱藏這樣工作。提前致謝。簡化並重寫我的重複代碼

<script> 
function delVesselAll(){ 
    $("#vessel_tab #father1").remove(); 
    $("#vessel_tab .son1").remove(); 
    document.getElementById(id).style.display = 'block'; 
}; 
function delVesselAll2(){ 
    $("#vessel_tab #father2").remove(); 
    $("#vessel_tab .son2").remove(); 
    document.getElementById(id).style.display = 'block'; 
}; 
</script> 

而且我的html:

<td class="cell_50"> 
    <a style="text-decoration:none;" onclick="showModal('delAll1')"><img src="images/delete-row-icon1.png" title="Delete this row" height="12" width="12" class="father1Del" id="father1Del"/></a> 
</td> 
<div class="delModal" style="z-index:999999; margin-left:200px; margin-top:30px; display:none" id="delAll1"> 
    <img src="images/warning.png" />&nbsp;Are you sure you want to delete vessel and the corresponding tanks?<br /> 
    <input type="button" value="Cancel" class="hide" onclick="hideModal('delAll1')"/> 
    <input type="button" value="Delete" onclick="delVesselAll()"/> 
</div> 

所以我基本上是做同樣的事情很多行,可以有類或ID 「father1」 「father2」 「SON1」 「son2」。此外,我想這些div淡入淡出,使用類似$(this).fadeIn('slow'); 在此先感謝。

+1

您應該顯示完整的佈局。 '#vessel_tab'和co。在哪裏? – Alexander

回答

4

您可以在變量傳遞(可能通過一個循環,雖然這取決於如何生成你的號碼):

function delVesselAll(num){ 
    $("#vessel_tab #father" + num).remove(); 
    $("#vessel_tab .son" + num).remove(); 
    document.getElementById(id).style.display = 'block'; 
}; 

然後調用它像:

delVesselAll(1); 
delVesselAll(2); 

如果您的號碼是可預測的和連續的,你可以在一個循環中迭代它們:

for (var i = 1; i < $("[id^='father']").length; i++) { 
    delVesselAll(i); 
} 

如果那還不清楚,你可能想看看這個tutorial

+0

我是否需要將num設置爲變量?它似乎沒有工作。 – triplethreat77

+0

@ triplethreat77這是一個函數,所以你需要用你的數字來調用它。 – Igor

+0

請進一步解釋一下嗎? – triplethreat77