2012-01-22 17 views
0

我正在使用按鈕向我的表中添加一行。添加新行時輸入名稱上升1?

我希望3行文本框的輸入名字在行克隆時增加1。

這是我現在使用的腳本。 (它克隆行和下方增加了它,但它並不+1輸入名稱

<script type="text/javascript"> 
$(document).ready(function() { 
    $("#add").click(function() { 
$('#mytable tbody>tr:last').clone(true).insertAfter('#mytable tbody>tr:last'); 
$('#mytable tbody>tr:last #product').val(''); 
    $('#mytable tbody>tr:last #qty').val(''); 
     $('#mytable tbody>tr:last #remarks').val(''); 
      $('#mytable tbody>tr:last #add').val('+'); 
return false; 
    }); 
}); 

這是我的表

<table id="mytable" width="250px" border="0" cellspacing="0" cellpadding="0" style="text-align:center; padding-left:40px; padding-bottom:8px"> 
    <tr class="product"> 
    <td style="text-align:center;"><strong>Item</strong></td> 
    <td style="text-align:center;"><strong>Qty.</strong></td> 
    <td style="text-align:center;"><strong>Remarks</strong></td> 
    </tr> 
    <tr name="product" class="product"> 
    <td width="100px"><input type="text" width="100px" name="product" id="product" /></td> 
    <td width="5px" style="text-align:center; padding-left:2px; padding-right:2px;"><input type="text" width="5px" size="1" maxlength="2" name="qty" id="qty" /></td> 
    <td width="100px"><input type="text" width="100px" name="remarks" id="remarks" /></td> 
    <td><input type="submit" name="add" id="add" value="+" /></td> 
    </tr> 
    </table> 

的問題是:我會怎樣有輸入名稱「產品」「數量」「備註」更改爲「product1」「qty1」「remarks1」在下一行?以及2行之後的行3之後,以無限多的行數

謝謝你的採訪。

+1

請考慮發佈一個[JS小提琴](http://jsfiddle.net/)供我們試驗,並查看(相關摘錄)您的腳本和HTML,工作。 –

+0

http://jsfiddle.net/WMvRJ/ –

回答

0

您可以在javascript中有一個全局變量記錄行號,也可以使用javascript來解析前一行的數字。我會親自使用一個全局變量。

2

以下似乎工作,添加到您的click()處理程序:

$('#mytable tbody > tr:last input').each(
     function(){ 
      /* the following removes the numerical characters from the id */ 
      var id = this.id.replace(/[0-9]+/g,''); 
      /* this iterates through each id that starts with the same sequence 
       of characters */ 
      $('input[id^=' + id + ']').each(
       function(i){ 
        /* this sets the id to the sequence of characters, 
         plus the number of the current iteration of the 
         each() method */ 
        this.id = id + i; 
       }); 
     }); 

JS Fiddle demo


響應編輯從OP(下)發表評論:

...但ID 1我需要輸入名稱按1.如何將上去上升我改變這個來增加「名字」?

你可以做到這一點很容易不夠,才使name等於id在內each()電話:

$('#mytable tbody > tr:last input').each(
     function(){ 
      /* the following removes the numerical characters from the id */ 
      var id = this.id.replace(/[0-9]+/g,''); 
      /* this iterates through each id that starts with the same sequence 
       of characters */ 
      $('input[id^=' + id + ']').each(
       function(i){ 
        /* this sets the id to the sequence of characters, 
         plus the number of the current iteration of the 
         each() method */ 
        this.id = id + i; 
        this.name = this.id; 
       }); 
     }); 

http://jsfiddle.net/davidThomas/WMvRJ/4/

參考文獻:

+0

嘿,這真的很好。但id增加1我需要輸入名稱上升1.我將如何改變這個以增加「名稱」? –

+0

剛剛編輯瞭解決這個問題的答案,我的歉意:我真的應該想到這一點。 = / –

相關問題