2017-04-02 37 views
0

我已經制作了一個表,每個td中有一個按鈕,每個tr中有4個td,我想在按下相應按鈕時獲取td的序列,但是我得到序列0 -4,因爲家長在下一行之後改變。有沒有辦法從0-11獲得按鈕序列。這裏是我的jQuery代碼。當按下按鈕時獲取td的序列

$('.act-butn').on('click', function() { 
     var btnClicked = $(this).parent().index(); 
     $('.activity1').text(btnClicked); 
}); 

,這裏是我的表

<table class="beta" width=443 id="logButtons"> 
    <center> 
    <tr> 
     <td><input type='button' name='Production' value='Production' id="actB1" class="act-butn" /></td> 
     <td> <input type='button' name='Break' value='Break' id="actB2" class="act-butn" /></td> 
     <td> <input type='button' name='Medical' value='Medical' class="act-butn" /></td> 
     <td> <input type='button' name='Comfort Break' value='Comfort Break' class="act-butn" /></td> 
    </tr> 
    <tr> 
     <td> <input type='button' name='Meeting' value='Meeting' class="act-butn" /></td> 
     <td> <input type='button' name='Training' value='Training' class="act-butn" /></td> 
     <td> <input type='button' name='Business Improvement' value='Bus. Improvement' class="act-butn" /></td> 
     <td> <input type='button' name='Idle' value='Idle' class="act-butn" /></td> 
    </tr> 
    <tr> 
     <td> <input type='button' name='Downtime' value='Downtime' class="act-butn" /></td> 
     <td> <input type='button' name='Follow up' value='Follow up' class="act-butn" /></td> 
     <td> <input type='button' name='one-to-one' value='one to one' class="act-butn" /></td> 
     <td> <input type='button' name='Coaching' value='Coaching' class="act-butn" /></td> 
    </tr> 
    </center> 
</table> 

請提出修改意見。

回答

1

如果你想獲得<input>指數你需要的所有輸入指數集合對

var $inputs = $('.act-butn').on('click', function() { 
    // index of `this` in `$inputs` collection 
    var btnClicked = $inputs.index(this); 
    $('.activity1').text(btnClicked); 
}); 

或者,如果你希望所有<td><td>指數基於輸入點擊:

var $td = $('.act-butn').on('click', function() {   
    var btnClicked = $td.index($(this).parent()); 
    $('.activity1').text(btnClicked); 
}).parent(); 

DEMO

+0

感謝的人,它的工作。 –