2012-12-08 47 views
0

我試圖在觸發此事件時增加選定的invoiceLine元素的索引。我想知道是否有人可以給我一個簡單的方法來做到這一點,如果它甚至是可能的,因爲我是JQuery的新手,並且對此非常困難。每次觸發事件時更改所選元素的索引

$invoiceLine.find('input').change(function(){ 
    $(this).parent().parent().next().find('div input').removeAttr('disabled'); 
}); 
+0

我試圖增加所選invoiceLine元素每次的「變」事件被觸發的索引。所以基本上,每次事件運行時,'this'的索引都等於this + 1.因此,它在每次觸發時都將DOM中的'next'元素作爲目標。 – AnchovyLegend

+0

您是否想說要更改'$ invoiceLine'引用哪個元素?你顯示的代碼沒有「索引」變量,所以你的問題沒有意義。 「選擇」是什麼意思? – nnnnnn

+1

有如此多的'.parent()','next()','find()'等等,HTML **是**相關的。 – Abhilash

回答

0

如果索引你的意思是它的兄弟姐妹中的元件的位置,就可以容易地移動與許多jQuery方法,例如元素後(),()之前,insertAfter(),的insertBefore(),追加(),appendTo()等 下面是一個簡單的例子:

HTML:

<div class="invoiceLine"> 
    1 <input /> 
</div> 
<div class="invoiceLine"> 
    2 <input /> 
</div> 
<div class="invoiceLine"> 
    3 <input /> 
</div> 

JS:

$('.invoiceLine').find('input').change(function() { 
    var line = $(this).closest('.invoiceLine'); // find enclosing invoiceLine 
    var next = line.next(); // find next invoiceLine 
    if (next.length) // check that current line is not last 
     line.insertAfter(next); // insert current line after the next one 
});​​​​​​​​​​​​​​ 

小提琴:http://jsfiddle.net/antishok/DteuU/

+0

這不是我要求的,不過謝謝。 – AnchovyLegend

+0

然後我完全不知道你在問什麼;) – antishok

相關問題