2017-01-01 34 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

固定他們兩個,還是有同樣的問題 –