删除生成的字段javascript


Remove generated fields javascript

使用js函数删除字段时遇到问题。首先,我在数据库表中生成尽可能多的条目:

function queryComposant($param) {
    $query = "SELECT nom_" . $param . " FROM " . $param . ";";
    $result = connectdb()->query($query)or die("Err " . $query);
    $resultat = $result->fetch(PDO::FETCH_OBJ);
    echo '<table border = "1" id="table' . $param . '">';
    foreach ($resultat as $value) {
    }
    while ($resultat = $result->fetch(PDO::FETCH_OBJ)) {
        echo '<tr>'
        . '<form action="' . $param . 'Delete.php"  method="post">';
        $n = "nom_" . $param;
        $nom = $resultat->$n;
        echo '<td>' . $nom . '</td>'
        . '<input id="' . $param . 'delete" type="hidden" name="delete" value="'.$nom.'"    />'
        . '<td><input type="button"  onclick="remove();" value="Supprimer" /></td>'
        . '</form>'
        . '</tr>';
    }
    echo '</table>';
}

我想每次点击"Supprimer"按钮时都删除表中的整个CCD_ 1。

function remove(){
    $().live('click',function(){
        var whichtr = $(this).parent().parent();
        whichtr.remove(); 
    });
}

我做错了什么?我使用$(this).parent().parent();button转到td,从td转到tr,然后尝试删除它,但该功能只删除了按钮。

我也尝试过这个,但没有成功:

function remove(){
    $().live('click',function(){
        var whichtr = $(this).closest("tr");
        whichtr.remove(); 
    });
}

由于内联javascript事件处理程序被认为是不好的做法,我删除了它们,并在javascript本身中创建了一个点击事件处理程序。

假设这个示例html:

<table>
    <tr>
        <form>
        <td></td>
        <input type="hidden" />
        <td>
        <input type="button"  class="remove" value="Supprimer" />            
        </td>
        </form>
    </tr>    
    <tr>
        <form>
        <td></td>
        <input type="hidden" />
        <td>
        <input type="button"  class="remove" value="Supprimer" />            
        </td>
        </form>
    </tr>
</table>

以下js将起作用:

$('table').on('click', '.remove', function() {
    $(this).closest('tr').remove();
});

工作示例

您不必创建一个处理程序来绑定"supprimer"上的点击事件,只要您将此事件直接绑定到按钮的onclick方法

所以你可以写这样的东西:

function remove(){
   var whichtr = $(this).closest("tr");
   whichtr.remove(); 
}

(未测试)

我希望它能帮助

尝试低于

<input type="button"  onclick="remove(this);" value="Supprimer" />

删除方法如下:-

function remove(e){
      $(e).closest('tr').remove();
}

从问题复制:

如果我在生成的表单中直接包含js代码,则有效:

function queryComposant($param) {
    $query = "SELECT nom_" . $param . " FROM " . $param . ";";
    $result = connectdb()->query($query)or die("Err " . $query);
    $resultat = $result->fetch(PDO::FETCH_OBJ);
    echo '<table border = "1" id="table' . $param . '">';
    foreach ($resultat as $value) {
    }
    while ($resultat = $result->fetch(PDO::FETCH_OBJ)) {
        echo '<tr>'
        . '<form action="' . $param . 'Delete.php"  method="post">';
        $n = "nom_" . $param;
        $nom = $resultat->$n;
        echo '<td>' . $nom . '</td>'
        . '<input id="' . $param . 'delete" type="hidden" name="delete"       value="'.$nom.'"     />'
        . '<td>'
        . '<input type="button" onclick="$(''table'').on(''click'', ''.remove'', function() {'
        . '$(this).closest(''tr'').remove();});" class="remove" value="Supprimer" />'
        . '</td>'
        . '</form>'
        . '</tr>';
    }
    echo '</table>';
}