2017-01-01 74 views
0

我只是在另一个看到张贴的解决方案,但是当我尝试在我的代码中使用它,它不工作。克隆antoher

我试图通过按钮在一个表上添加项目到另一个。任何帮助表示赞赏。如预期

HTML

<div id="container"> 

    <input type="text" name="search" id="search" placeholder="Ingrese el nombre del examen..." size="50"> 
    </input> 

    <table id="proctable" > 
     <tr> 
      <th>Examen</th> 
      <th>Precio</th> 
      <th></th> 
     </tr> 

     <tr> 
      <td>hola</td> 
      <td>10</td> 
      <td><input class="addBtn" type="button" value="Agregar"></td> 
     </tr> 

    </table> 

    <hr> 

    <div> 
     <table id="comptable"> 
      <tr class="header"> 
       <th>Examen</th> 
       <th>Precio</th> 
      </tr> 
     </table> 
    </div> 

</div> 

JS

<script type="text/javascript"> 
    var items = []; 

     $(".addBtn").on("click", function() { 
      var newTr = $(this).closest("tr").clone(); 
      items.push(newTr); 
      newTr.appendTo($("#comptable")); 

     }); 
</script> 

https://jsfiddle.net/45nfvg9j/

回答

0

您的JavaScript实际工作。由于两件小事情,jsFiddle被打破了:

  1. 你有脚本打开和关闭你的javascript标签。 jsFiddle实际上显示了一个黄色的音符“输入纯JavaScript代码,没有HTML”。

  2. 输入标签需要自闭,就像这样:<input class="addBtn" type="button" value="Agregar"/>,不<input class="addBtn" type="button" value="Agregar"></input>

+0

你好,感谢你。我从javascript中打开和关闭标签,代码仍然无法使用。 –

+0

如果您也修复了问题#2,则代码有效。查看jsFiddle中的html - 无效的输入标记以红色突出显示! –

+0

固定他们两个,还是有同样的问题 –