Jquery: 在包含特定列值的行后插入新行
Jquery: Insert a new row after a row that contain specific column value
如何使用 Jquery 在具有特定列值的行之后追加新行?
例如,我希望在 'Selina' 之后追加一行新的 [NAME: CINDY AGE: 21] 列,因为我想追加一个按 'AGE' 和 [NAME: Tori AGE: 排序的新行: 26] 将附加在 'Kelly'.
之后
<table>
<tr>
<th>NAME</th>
<th>AGE</th>
</tr>
<tbody>
<tr>
<td>Alice</td>
<td>18</td>
</tr>
<tr>
<td>Selina</td>
<td>20</td>
</tr>
<tr>
<td>Kelly</td>
<td>25</td>
</tr>
</tbody>
</table>
<input type="text" name="name" id="name" placeholder="NAME">
<input type="text" name="age" id="age" placeholder="AGE">
<button type="button" id="add_btn">ADD</button>
谢谢。加油:)
这应该有效:
jQuery('#add_btn').on('click',function(){
var age = jQuery('#age').val();
var name = jQuery('#name').val();
var target = jQuery('tbody > tr > td:last-child').filter(function(){
return jQuery(this).text() >= age;
}).first().parent();
if(target.length > 0)
target.before('<tr><td>'+name+'</td><td>'+age+'</td></tr>');
else
jQuery('tbody > tr:last').after('<tr><td>'+name+'</td><td>'+age+'</td></tr>');
});
function myFunction() {
document.getElementById('test').innerHTML = document.getElementById('test').innerHTML + "<tr><td>" + document.getElementById('name').value + "</td><td>" + document.getElementById('age').value + "</td></tr>";
}
<!DOCTYPE html>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<table id="test">
<tr>
<th>NAME</th>
<th>AGE</th>
</tr>
<tbody>
<tr>
<td>Alice</td>
<td>18</td>
</tr>
<tr>
<td>Selina</td>
<td>20</td>
</tr>
<tr>
<td>Kelly</td>
<td>25</td>
</tr>
</tbody>
</table>
<input type="text" name="name" id="name" placeholder="NAME">
<input type="text" name="age" id="age" placeholder="AGE">
<button type="button" onclick="myFunction()">ADD</button>
</body>
</html>
如何使用 Jquery 在具有特定列值的行之后追加新行?
例如,我希望在 'Selina' 之后追加一行新的 [NAME: CINDY AGE: 21] 列,因为我想追加一个按 'AGE' 和 [NAME: Tori AGE: 排序的新行: 26] 将附加在 'Kelly'.
<table>
<tr>
<th>NAME</th>
<th>AGE</th>
</tr>
<tbody>
<tr>
<td>Alice</td>
<td>18</td>
</tr>
<tr>
<td>Selina</td>
<td>20</td>
</tr>
<tr>
<td>Kelly</td>
<td>25</td>
</tr>
</tbody>
</table>
<input type="text" name="name" id="name" placeholder="NAME">
<input type="text" name="age" id="age" placeholder="AGE">
<button type="button" id="add_btn">ADD</button>
谢谢。加油:)
这应该有效:
jQuery('#add_btn').on('click',function(){
var age = jQuery('#age').val();
var name = jQuery('#name').val();
var target = jQuery('tbody > tr > td:last-child').filter(function(){
return jQuery(this).text() >= age;
}).first().parent();
if(target.length > 0)
target.before('<tr><td>'+name+'</td><td>'+age+'</td></tr>');
else
jQuery('tbody > tr:last').after('<tr><td>'+name+'</td><td>'+age+'</td></tr>');
});
function myFunction() {
document.getElementById('test').innerHTML = document.getElementById('test').innerHTML + "<tr><td>" + document.getElementById('name').value + "</td><td>" + document.getElementById('age').value + "</td></tr>";
}
<!DOCTYPE html>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<table id="test">
<tr>
<th>NAME</th>
<th>AGE</th>
</tr>
<tbody>
<tr>
<td>Alice</td>
<td>18</td>
</tr>
<tr>
<td>Selina</td>
<td>20</td>
</tr>
<tr>
<td>Kelly</td>
<td>25</td>
</tr>
</tbody>
</table>
<input type="text" name="name" id="name" placeholder="NAME">
<input type="text" name="age" id="age" placeholder="AGE">
<button type="button" onclick="myFunction()">ADD</button>
</body>
</html>