2017-05-10 9 views
-1

私は入力があるテーブルを持っており、使用している入力のインデックスを取得しようとしています。例えば要素を見つけるJQueryがリストにあります

$(document).on('keyup', '.inputs input', function(event) { 
 
    var inputVal = $(this).val(); 
 
    var inputListVal = $(this).parent('tr').index(this); <!-- This is the bit thas not working --> 
 
    console.log(inputListVal); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table> 
 
    <tr> 
 
    <th><input type="text" name=""></th> 
 
    <th><input type="text" name=""></th> 
 
    <th><input type="text" name=""></th> 
 
    <!-- I want this index if they are typing init --> 
 
    <th><input type="text" name=""></th> 
 
    </tr> 
 
</table>

答えて

2

について はTR要素にclosest()/.parents()を使ってトラバース。ゼロベースのインデックス

+1

は、すべての作業になりましたありがとうござい簡単な答えです –

2

試してみてください。入力のインデックスを取得するにはは注意index()

使用

var inputListVal = $(this).closest('tr').find('input').index(this); 

$(document).on('keyup', 'input', function(event) { 
 
    var inputListVal = $(this).closest('tr').find('input').index(this); 
 
    console.log(inputListVal); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table> 
 
    <tr> 
 
    <th><input type="text" name=""></th> 
 
    <th><input type="text" name=""></th> 
 
    <th><input type="text" name=""></th> 
 
    <!-- I want this index if they are typing init --> 
 
    <th><input type="text" name=""></th> 
 
    </tr> 
 
</table>

を使用し、その後、親に入力を取得しますこの$(this).parent('tr th').index();そしてkeyupイベントはあなたのhtmlにはありませんclass="inputs"がありません。 tr thその本

0

$(document).on('keyup', 'input', function(event) { 
 
    var inputVal = $(this).val(); 
 
    var inputListVal = $(this).parent().index(); 
 
    console.log(inputListVal); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table> 
 
    <tr> 
 
    <th><input type="text" name=""></th> 
 
    <th><input type="text" name=""></th> 
 
    <th><input type="text" name=""></th> 
 
    <!-- I want this index if they are typing init --> 
 
    <th><input type="text" name=""></th> 
 
    </tr> 
 
</table>

からthのインデックスを見つけます

これは

0

$(document).on('keyup', 'input', function(event) { 
 
    var inputListVal = $('input').index(this); 
 
    console.log(inputListVal); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table> 
 
    <tr> 
 
    <th><input type="text" name=""></th> 
 
    <th><input type="text" name=""></th> 
 
    <th><input type="text" name=""></th> 
 
    <!-- I want this index if they are typing init --> 
 
    <th><input type="text" name=""></th> 
 
    </tr> 
 
</table>
を試してみてください tr

$(document).on('keyup', 'input', function(event) { 
 
    var inputVal = $(this).val(); 
 
    var inputListVal = $(this).parent('tr th').index(); 
 
    console.log(inputListVal); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table> 
 
    <tr> 
 
    <th><input type="text" name=""></th> 
 
    <th><input type="text" name=""></th> 
 
    <th><input type="text" name=""></th> 
 
    <!-- I want this index if they are typing init --> 
 
    <th><input type="text" name=""></th> 
 
    </tr> 
 
</table>

関連する問題