Jquery - 将一行从一个表复制到另一个表

时间:2016-08-30 11:25:43

标签: javascript jquery html

我是jquery和javascript的新手,我正在尝试编写一个简单的应用程序,当我按下按钮时,将一行从一个表复制到另一个表,然后从中删除该行第一张桌子。我有一个可以正常工作的DeleteRow功能,但我无法通过我的" DraftPlayer" 功能来复制该行。我已经尝试了很多我在网络上找到的解决方案,但我无法正确理解语法。在我解决这个问题时,您只会看到表格中的第二行有DraftPlayer按钮。

以下是我认为至关重要的代码段:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
//<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>

<body>
<div>&nbsp;</div>
<p>Welcome to Mike's Draft App! </p>
<table id="players">
<table border="1">
  <tr>
    <td width="36"> Rank </td>
    <td width="141"> Player Name </td>
    <td width="52">Position </td>
    <td width="38">Team </td>
    <td width="69"> Bye Week </td>
    <td width="52"><input type="button" value="Delete" onClick="DeleteRow(this)"></td>
  </tr>
  <tr>
    <td>1</td>
    <td>Antonio Brown</td>
    <td>WR</td>
    <td>PIT</td>
    <td>8</td>
    <td><input type="button" value="Delete" onClick="DeleteRow(this)"></td>
    <td width="103">&nbsp;</td>
  </tr>
  <tr>
    <td>2</td>
    <td>Julio Jones</td>
    <td>WR</td>
    <td>ATL</td>
    <td>11</td>
    <td><input type="button" value="Delete" onClick="DeleteRow(this)"></td>
    <td><input type="button" value="Draft" onClick="DraftPlayer(this)"></td>
  </tr>

<table id="drafted">
<table border="1">
  <tr>
    <td width="36"> Rank </td>
    <td width="141"> Player Name </td>
    <td width="52">Position </td>
    <td width="38">Team </td>
    <td width="69"> Bye Week </td>
  </tr>
  </table>


<script>
    function DeleteRow(o) {
     var p=o.parentNode.parentNode;
         p.parentNode.removeChild(p);
    }

    function DraftPlayer(o) {
     var p=o.parentNode.parentNode;

    copyTable = $('#drafted tbody');
    cloneRow = p.clone();
    copyTable.append(cloneRow);

    p.parentNode.removeChild(p);     

    }
</script>
</body>
</html>

感谢您的任何建议!

3 个答案:

答案 0 :(得分:1)

您正在选择DOM中不存在的$('#drafted tbody');,这样您就什么也得不到。您需要将tbody添加到表格中,或者需要更改选择器。

<table id="drafted">
 <tbody>
   <tr>
    <th width="36"> Rank </th>
    <th width="141"> Player Name </th>
    <th width="52">Position </th>
    <th width="38">Team </th>
    <th width="69"> Bye Week </th>
   </tr>
  </tbody>
</table>

function DraftPlayer(o) {
        var p = $(o).closest('tr');
        copyTable = $('#drafted tbody');
        cloneRow = p.clone();
        copyTable.append(cloneRow)
        p.remove();

    }

答案 1 :(得分:0)

&#13;
&#13;
    function DeleteRow(o) {
     var p=o.parentNode.parentNode;
         p.parentNode.removeChild(p);
    }

    function DraftPlayer(o) {
     var p=o.parentNode.parentNode;

    copyTable = $('#drafted tbody');
    cloneRow = p.innerHTML;

//alert(cloneRow )
    copyTable.append("<tr>"+cloneRow+"</tr>");

    p.parentNode.removeChild(p);     

    }
&#13;
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
//<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>

<body>
<div>&nbsp;</div>
<p>Welcome to Mike's Draft App! </p>
<table id="players">
<table border="1">
  <tr>
    <td width="36"> Rank </td>
    <td width="141"> Player Name </td>
    <td width="52">Position </td>
    <td width="38">Team </td>
    <td width="69"> Bye Week </td>
    <td width="52"><input type="button" value="Delete" onClick="DeleteRow(this)"></td>
  </tr>
  <tr>
    <td>1</td>
    <td>Antonio Brown</td>
    <td>WR</td>
    <td>PIT</td>
    <td>8</td>
    <td><input type="button" value="Delete" onClick="DeleteRow(this)"></td>
    <td width="103">&nbsp;</td>
  </tr>
  <tr>
    <td>2</td>
    <td>Julio Jones</td>
    <td>WR</td>
    <td>ATL</td>
    <td>11</td>
    <td><input type="button" value="Delete" onClick="DeleteRow(this)"></td>
    <td><input type="button" value="Draft" onClick="DraftPlayer(this)"></td>
  </tr>


<table id="drafted" border="1">
  <tr>
    <td width="36"> Rank </td>
    <td width="141"> Player Name </td>
    <td width="52">Position </td>
    <td width="38">Team </td>
    <td width="69"> Bye Week </td>
  </tr>
  
</table>


</body>
</html>
&#13;
&#13;
&#13;

您的代码中存在很多错误。浏览我的代码,编辑它,工作正常

答案 2 :(得分:0)

您需要将DOM元素转换为jquery对象才能使用clone()函数。 修改后的DraftPlayer()函数是:

function DraftPlayer(o) {
 var p=o.parentNode.parentNode;

copyTable = $('#drafted tbody');
cloneRow = $(p).clone();
copyTable.append(cloneRow);

p.parentNode.removeChild(p);     

}

<强>建议:
你的HTML也存在一些问题 修改表格标签。

<table id="your_id" border="1">
       <tbody>
         -----------
         -----------
      </tbody>
</table>