你的问题是你的班级命名。看到这个工作小提琴http://jsfiddle.net/ZFWU7/5/
通过命名td和输入.form_id
您正在迭代所有这些。
$('td.form_id').each(function(i){
$(this).text(i+1);
});
让您选择更具体的
按你的意见,我会分开你的“排序”功能到它自己的明确定义的函数,而不是使这两种行为anonomys功能的一部分。你没有很好地遵循DRY规则。我已经用更清晰的可维护方式(从小提琴)重写了您的代码。这只是稍微好一点。在全局名称空间中声明函数并不是一个很好的做法。命名空间的'控制器'组会更好。 http://addyosmani.com/blog/essential-js-namespacing/
$(document).ready(function() {
$("#add-line").click(addRow);
});
function updateRowOrder(){
$('td.form_id').each(function(i){
$(this).text(i+1);
});
}
function addRow(){
var template = $('#template'),
id = 0;
if(!template.is(':visible'))
{
template.show();
return;
}
var row = template.clone();
template.find("input:text").val("");
row.attr('id', 'row_' + (++id));
template.before(row);
var i=1;
updateRowOrder();
$('.form-fields').on('click', '.remove', removeRow);
}
function removeRow(){
var row = $(this).closest('tr');
if(row.attr('id') == 'template')
{
row.hide();
}
else
{
row.remove();
}
updateRowOrder();
}
是的你是对的。但是从1开始直接怎么样?我的意思是第一行没有输入框。而且,当我们从中间移除时,它应该减少名单上的总数。就像我们删除#3时列出的那样:1-2-4 –
看到更新以上 – Brad
新的小提琴btw http://jsfiddle.net/ZFWU7/6/ – Brad