动态添加行时添加id

时间:2014-05-24 20:02:43

标签: javascript

当我动态添加行时,如何为每行添加id?

到目前为止,我有这个。我不知道如何去寻找身份证。我怎么能这样使用克隆而不是复制整个东西并每次都放i

<table id="attr_add">
               <tr>
                   <td><?php echo CHtml::activeTextField($attr,'['.$i.']name',array('class'=>'span12','placeholder'=>'Red or X-Small')); ?></td>
                   <td><?php echo CHtml::activeTextField($attr,'['.$i.']cost',array('class'=>'span4','placeholder'=>'89.99')); ?></td>
                   <td><?php echo CHtml::activeTextField($attr,'['.$i.']value',array('class'=>'span12','placeholder'=>'#ff0000 or Small')); ?></td>
                   <td><input type="button" name="addRow[]" class="removeRow" value='-' />
                  <td> <input type="button" name="addRow[]" class="add" value='+' />
               </tr>
         </table>     

         <script>
         $(document).ready(function () {
         var id = 0;
            $(document).on('click', '#attr_add .add', function () {
                id++;

                if(id <7){
                var row = $(this).closest('tr');
                var clone = row.clone();
                var tr = clone.closest('tr');

                tr.find('input[type=text]').val('');
                $(this).closest('tr').after(clone);                  
                }});

            $(document).on('click', '#attr_add .removeRow', function () {
                if ($('#attr_add .add').length > 1) {
                    $(this).closest('tr').remove();
                }
            });
        });

0 个答案:

没有答案