2014-04-17 46 views
0

我有一個表如何將類添加到使用jQuery的asp.net mvc中動態創建的行?

<table id="dataTable" style="width: 80%; border: none"> 
<tbody> 
    <tr class="CurrentRow"> 
     <td> 
      @Html.TextBoxFor(m => m.GetTimeSheetDetails[i].SUN_HRS, new { style = "width:50px; height:30px;", @class = "sunhrs" } 
     </td> 
    </tr> 
</tbody></tr></table> 

和另一個表

<table> 
<tbody> 
    <tr class="AddRow> 
     <td> 
      @Html.TextBoxFor(m => m.GetTimeSheetDetails[i].SUN_HRS, new { style = "width:50px; height:30px;" } 
     </td> 
    </tr> 
</tbody></tr></table> 

現在我加入行 「Currentclass」 使用 「Addrow」 類像

$("#btnAdd").click(function() { 

    var template = $('.AddRow').clone() 

    template.find('input[type=text]').val(''); 
    $.each(template.find('input[type=text]'), function() { 
     var name = $(this).attr('name'); 
     name = name.replace('0', count - 1); 
     $(this).attr('name', name); 
    }); 

    $("#dataTable").append(template); 
    template.removeClass('AddRow').addClass('CurrentRow').show(); 

}); 

現在,我想在按鈕點擊的新創建的行文本框中添加'sunhrs'作爲'數據表'。我試過了,就像

var currentTemplate = $("#dataTable").clone(); 
currentTemplate.find('input[type=text]').addClass('sunhrs') 

但是不行,請任何人幫助我。

+0

也許一個錯字,但你有一個額外的結局'你TBODY和表結束標籤之間tr'標籤。 –

回答

0

試試這個:

$("#dataTable").append(template); 
$("#dataTable .AddRow").addClass('CurrentRow').removeClass('AddRow').show(); 
+0

添加行,但不向文本框中添加類「sunhrs」? @Milind Anantwar – Sanjay

+0

追加內容後再試。 –

+0

不,不工作... – Sanjay

0

此代碼工作細

$("#dataTable").append(template); 
$("#dataTable .AddRow").find('input[type=text]').addClass('sunhrs'); 
$("#dataTable .AddRow").addClass('CurrentRow').removeClass('AddRow').show(); 
相關問題