2016-01-07 44 views
0

我想弄清楚如何讓這個腳本在第一個表格行之前添加一行,並且允許」回車「鍵循環在細胞之間。這裏是我到目前爲止,增加了一個行每行後在表格之前添加新的表格行並在行之間輸入「

var i=$('table tr').length+1; 
$(".addmore").on('click',function(){ 
    addNewRow(); 
}); 

$(document).on('keypress', ".addNewRow", function(e){ 
    var keyCode = e.which ? e.which : e.keyCode; 
    if(keyCode == 9) addNewRow(); 
}); 

var addNewRow = function(id){ 
    html = '<tr id="tr_'+i+'">'; 
    html += '<td><input class="case" id="caseNo_'+i+'" type="checkbox"/></td>'; 
    html += '<td class="prod_c"><input type="text" data-type="product" name="data[product]['+i+'][product_id]" id="product_'+i+'" class="form-control autocomplete_txt" autocomplete="off">'; 
    html +='</td>'; 
    html += '<td><input type="checkbox" name="data[InvoiceDetail]['+i+'][added]" id="added_1'+i+'" class="form-control autocomplete_txt" autocomplete="off"></td>'; 
    html += '</select></td>'; 
    html += '</tr>'; 
    if(typeof id !== "undefined"){ 
     $('#tr_'+id).after(html); 
    }else{ 
     $('table').append(html); 
    } 
    console.log(id); 

    $('#caseNo_'+i).focus(); 

    i++; 

回答

2

而不是「追加」,使用「前置」,這將解決這一問題

$('table').prepend(html); 
+0

NM,固定,結果發現我無法拼寫前綴:P – Steven

+0

你知道我可以改變以允許「輸入」在單元之間循環嗎? – Steven

+0

您是否在點擊「enter」時添加新行? – Thanga