你的問題是你的班級命名。看到這個工作小提琴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