动态表行创建

时间:2017-02-25 07:27:12

标签: javascript jquery html html-table

我正在尝试在表格中插入动态行。显然,我能够添加输入行,但我无法为按钮<td class="text-center"><a class='btn btn-info btn-xs' href="#"><span class="glyphicon glyphicon-edit"></span> Select</a> </td>添加新行。它给出了Uncaught SyntaxError: missing ) after argument list

的错误
<table id="tab_logic" class="table table-bordered table-striped display nowrap" cellspacing="3" width="100%">
   <br>
   <thead>
      tr>
      <th>Name</th>
      <th>Action</th>
      </tr>
   </thead>
   <tbody>
      <tr id='addr0'>
         <td>1
         </td>
         <td class="text-center"><a class='btn btn-info btn-xs' href="#"><span class="glyphicon glyphicon-edit"></span> Select</a> </td>
      </tr>
      <tr id='addr1'></tr>
   </tbody>
   <tfoot>
   </tfoot>
</table>

的Javascript

<script type="text/javascript">
$(document).ready(function(){
    var i=1;
    $("#add_row").click(function(){
        $('#addr'+i).html("<td>"+ (i+1) +"</td><td><input name='name"+i+"' type='text' placeholder='Name' class='form-control input-md'  /> </td>   <td class="text-center"><a class='btn btn-info btn-xs' href="#"><span class="glyphicon glyphicon-edit"></span> Select</a> </td>");
        $('#tab_logic').append('<tr id="addr'+(i+1)+'"></tr>');
        i++; 
    });
});
</script>

2 个答案:

答案 0 :(得分:0)

使用这个,你把&#34;代替&#39; ...并在html中检查行<tr>,但显示tr&gt;

var i=1;
     $("#add_row").click(function(){
      $('#addr'+i).html("<td>"+ (i+1) +"</td><td><input name='name"+i+"' type='text' placeholder='Name' class='form-control input-md'  /> </td>   <td class='text-center'><a class='btn btn-info btn-xs' ><span class='glyphicon glyphicon-edit'></span> Select</a> </td>");
      $('#tab_logic').append('<tr id="addr'+(i+1)+'"></tr>');
      i++; 
  });

答案 1 :(得分:0)

尽量让您的"'保持清洁。你有很多混在一起

&#13;
&#13;
var i = 1;
$('#add_row').click(
  function(){
    $('#addr'+i).html('<td>' + (i+1) + '</td><td class="text-center"><a class="btn btn-info btn-xs" href="#"><span class="glyphicon glyphicon-edit"></span>Select</a></td>');
    $('#tab_logic').append('<tr id="addr'+(i+1)+'"></tr>');
    i++; 
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="tab_logic" class="table table-bordered table-striped display nowrap" cellspacing="3" width="100%">
 <thead>
 <tr>
 <th>Name</th>
 <th>Action</th>
 </tr>
 </thead>
 <tbody>
 <tr id='addr0'>
 <td>1
 </td>              
 <td class="text-center"><a class="btn btn-info btn-xs" href="#"><span class="glyphicon glyphicon-edit"></span> Select</a> </td>    
  </tr>
  <tr id="addr1"></tr>                          
  </tbody>          
  <tfoot>
  </tfoot>
  </table>
  
  <button id="add_row">Add Row</button>
&#13;
&#13;
&#13;