1
<html>
<head>
<script type="text/javascript">
function removeLink(i)
{
    document.getElementById("tab2").deleteRow(i);
}
</script>


</head>
<body>
<form action="forth.php" method="post">
<table width="600" border="1" id="tab2">


<?php

    foreach($_POST as $key => $post2)
    {
    ?>
    <tr>
    <td>
    <?php
    echo $post2.'<br />';
    ?>
    <input type="hidden" name="<?php echo $key;?>" value="<?php echo $post2;?>" />

    </td>
    <td><a href="#" onClick="removeLink(this.parentNode.parentNode.rowIndex);">Remove</a></td>
    </tr>
    <?php

    }

    ?>
    <tr>
    <td><input type="submit" value="Next" /></td>
    <td>&nbsp;</td>
  </tr>
  </table>

</form>
</body>

私のアンカータグがremoveLink()のonclick関数を持っているのを見ることができますが、期待どおりにtr全体を削除しません。アンカーが生成したリンクをクリックしても、アクションは実行されません。removeLink(this.parentNode.parentNode.rowIndex) で定義された内部オブジェクトをサポートしていないアンカーのような問題はありますか? みんなそれができる方法を助けます

4

2 に答える 2

1

Jクエリを使用します。含む<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

そして、これを試してください

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $('.deleteMe').click(function(){
        $(this).parent().parent().remove();
        //or u can hide that like $(this).parent().parent().hide.();
    })
});

</script>


</head>
<body>
<form action="forth.php" method="post">
<table width="600" border="1" id="tab2">


<?php

    foreach($_POST as $key => $post2)
    {
    ?>
    <tr>
    <td>
    <?php
    echo $post2.'<br />';
    ?>
    <input type="hidden" name="<?php echo $key;?>" value="<?php echo $post2;?>" />

    </td>
    <td><a href="#" class="deleteMe">Remove</a></td>
    </tr>
    <?php

    }

    ?>
    <tr>
    <td><input type="submit" value="Next" /></td>
    <td>&nbsp;</td>
  </tr>
  </table>

</form>
</body>
于 2012-05-04T05:34:51.890 に答える
0

アンカー onclick 行のコードの下の最初の変更。

onClick="removeLink(this);">

それからこれを試してください。

<script type="text/javascript">
function removeLink(obj){
var par=obj.parentNode;
while(par.nodeName.toLowerCase()!='tr'){
par=par.parentNode;
}
i = par.rowIndex;
document.getElementById("tab2").deleteRow(i);
}
</script> 
于 2012-05-04T05:37:05.940 に答える