jquery 增加 <tr><td></td>....</tr>

解决方案 »

  1.   


    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
     <head>
      <meta http-equiv="content-type" content="text/html; charset=utf-8">
      <title> jquery demo </title>
      <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
     </head> <body>
      <table id="tb">
    <tr>
    <td>name</td>
    <td>age</td>
    </tr>
    <tr>
    <td><input type="text" value="1"></td>
    <td><input type="text" value="1"></td>
    </tr>
      </table>  <p><input type="button" value="Add" onclick="add()"> <input type="text" name="addnum" id="addnum"> #row</p>
      
      <script type="text/javascript">
    function add(){ var pattern = /^\d+$/;
    if(!pattern.test($('#addnum').val())){
    alert('num error');
    return false;
    } var tmp = '';
    for(var i=0; i<$('#addnum').val(); i++){
    tmp += ' <tr><td><input type="text" value=""></td><td><input type="text" value=""></td></tr>';
    } $('#tb').append(tmp); }
      </script> </body>
    </html>