如何在新创建行的文本框中设置用户给定值的索引,并将其附加到下面的表中,但如何在表中的每行第一列中显示该索引?我对这种类型的功能还不熟悉。
$(document).ready(function(){
$('#addbtn').click(function(){
var s = $.trim($('#user').val());
var i = 0;
var c = 1;
var count = i+c;
if(s !=''){
$('#result').attr('src','http://www.clipartbest.com/cliparts/abT/y4b/abTy4bcL7.png');
$('#usertbl').append('<tr height=\"30\">'+'<td>'+count+'</td>'+'<td>'+s+'</td>'+'<td>'+'</td>'+'</tr>');
}
$('#user').val(' ');
});
});table,tr,th,td{
border:1px solid #dddddd;
border-collapse:collapse;
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="user"/>
<button id="addbtn">Add</button><span><img src="http://pix.iemoji.com/images/emoji/apple/ios-9/256/cross-mark.png" id="result" height="50" width="50"/></span>
<table style="width:100%;" id="usertbl">
<tr height="30">
<th width="34%">Serial Number</th>
<th width="33%">User NAme</th>
<th width="33%">Content</th>
</tr>
</table>
发布于 2016-09-22 12:54:46
$(document).ready(function(){
var count = 1;
$('#addbtn').click(function(){
var s = $.trim($('#user').val());
if(s !=''){
$('#result').attr('src','http://www.clipartbest.com/cliparts/abT/y4b/abTy4bcL7.png');
$('#usertbl').append('<tr height=\"30\">'+'<td>'+count+'</td>'+'<td>'+s+'</td>'+'<td>'+'</td>'+'</tr>');
count++;
}
$('#user').val(' ');
});
$('#user').keyup(function(){
$('#result').attr('src','http://pix.iemoji.com/images/emoji/apple/ios-9/256/cross-mark.png');
});
});table,tr,th,td{
border:1px solid #dddddd;
border-collapse:collapse;
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="user"/>
<button id="addbtn">Add</button><span><img src="http://pix.iemoji.com/images/emoji/apple/ios-9/256/cross-mark.png" id="result" height="50" width="50"/></span>
<table style="width:100%;" id="usertbl">
<tr height="30">
<th width="34%">Serial Number</th>
<th width="33%">User NAme</th>
<th width="33%">Content</th>
</tr>
</table>
click事件外部的Decalre变量,并在单击时使用增量重写它。
发布于 2016-09-22 12:58:05
声明我上面的click函数。
ie- i=0;
发布于 2016-09-22 12:59:43
您可以计算行数并将其用于索引
var $tbl = $('#usertbl');
var count = $('tr', $tbl).length;https://stackoverflow.com/questions/39630505
复制相似问题