2012-09-12 26 views
1

我有一個基於對問題的回答添加div容器的過程。用戶可以選擇在任何時候重新開始。我想在用戶這樣做後的某個點後刪除所有容器。例如,這裏是您在3次回覆後會看到的內容。刪除jQuery中的某個位置後的所有容器

<div id="SolutionAdvisor"> 
    <div id="Col1"><div class="content">some content here</div></div> 
    <div id="Col2"><div class="content">some content here</div></div> 
    <div id="Col3"><div class="content">some content here</div></div> 
</div> 

如果用戶決定返回到第2題本,我想看到什麼:GT指數,像這樣:

 $('#SolutionAdvisor div:gt('+column+')').remove(); 

<div id="SolutionAdvisor"> 
    <div id="Col1"><div class="content">some content here</div></div> 
    <div id="Col2"><div class="content">some content here</div></div> 
</div> 

我已經使用了嘗試還試過:

 $('#SolutionAdvisor div:gt('+column+')').each(function(){$(this).remove();}); 

where column woul d是1

我所看到的,而不是像這樣:

<div id="SolutionAdvisor"> 
    <div id="Col1"><div class="content">some content here</div></div> 
    <div id="Col2"><div class="content">some content here</div></div> 
    <div id="Col2"><div class="content">some content here</div></div> 
</div> 

什麼是這樣做的正確方法?

+0

你'prev'和'返回'按鈕? – Phil

+0

你確定'column'變量是你認爲的嗎?它是基於零的(如jQuery':gt()')?你可能會展示[現場演示](http://jsfiddle.net/)? –

+0

不,這些問題有一系列的響應下拉列表。 – septemberbrain

回答

1

您可以從父DIV HTML元素的數組讓孩子的名單。然後,包裹每一個在jQuery的電話,如下刪除:

var maxQuestions = 2; 

children = $('#SolutionAdvisor').children(); 
for (var i = maxQuestions; i < children.length; i++) { 
    $(children[i]).remove(); 
} 

小提琴:http://jsfiddle.net/stevenmtwhunt/YQusg/2/

有與jQuery選擇這樣做太的一些很酷的方式,我只是喜歡的概念簡單for循環。

+0

這對我有用。謝謝! – septemberbrain

0

你應該使用直接後代選擇,如:

$('#SolutionAdvisor > div:gt(?)).remove(): 

還要注意的是傳遞給gt()值爲零索引。

此外,您可能希望這些類div的,讓你可以直接訪問它們,如:

$('.responses:gt(?)').remove(); 
0

這是使用.nextAll()方法來捕獲所有輸入的值之後到來的元素最好的情況..

$('#txt1').on('change' , function() { 
     var quesNo = $(this).val();  
     $('#SolutionAdvisor div:nth-child('+quesNo+')').nextAll().remove();   
    }); 

檢查FIDDLE這裏

相關問題