使用jquery删除tr

时间:2015-11-04 04:15:31

标签: jquery html-table delete-row

我创建了一个表,想要用jquery删除行。

我到处寻找,我无法弄清楚我做错了什么。

<table id="ao-referrer-summary">
    <thead>
        <tr>
            <th>Referrer First Name</th>
            <th>Referrer Last Name</th>
            <th>Delete User</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>%%FNAME%%</td>
            <td>%%LNAME%%</td>
            <td>
                <button class="btnDelete">&times;</button>
            </td>
        </tr>
        <tr>
            <td>%%FNAME%%</td>
            <td>%%LNAME%%</td>
            <td>
                <button class="btnDelete">&times;</button>
            </td>
        </tr>
    </tbody>
</table>
var b = 70;

function myfunction() {
    b--;
    if (b < 3) {
        return b;
    } else {
        myfunction();
        return b;
    }
}
value = myfunction();
console.log(value);

小提琴 http://jsfiddle.net/designstreet1/4pvrtghu/

3 个答案:

答案 0 :(得分:2)

您的代码正常工作只需加载jQuery library即可使其正常运行。在左上角的下拉列表中选择jquery库。多数民众赞成如何使用JSFIDDLE。之后你很高兴去交配。见下面的截图:

enter image description here

如果您正在使用代码段,请在此处添加库:

enter image description here

正在使用 DEMO

答案 1 :(得分:1)

$(document).ready(function () {
    $("#ao-referrer-summary").on('click', '.btnDelete', function () {
        $(this).closest('tr').remove();
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table id="ao-referrer-summary">
    <thead>
        <tr>
            <th>Referrer First Name</th>
            <th>Referrer Last Name</th>
            <th>Delete User</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>%%FNAME%%</td>
            <td>%%LNAME%%</td>
            <td>
                <button class="btnDelete">&times;</button>
            </td>
        </tr>
        <tr>
            <td>%%FNAME%%</td>
            <td>%%LNAME%%</td>
            <td>
                <button class="btnDelete">&times;</button>
            </td>
        </tr>
    </tbody>
</table>

问题是缺少脚本。代码没问题

答案 2 :(得分:1)

您只需要包含jquery库以使js代码正常工作。

Screen shot here, and above is the updated fiddle link

&#13;
&#13;
$(document).ready(function () {
    $("#ao-referrer-summary").on('click', '.btnDelete', function () {
        $(this).closest('tr').remove();
    });
});
&#13;
<table id="ao-referrer-summary">
    <thead>
        <tr>
            <th>Referrer First Name</th>
            <th>Referrer Last Name</th>
            <th>Delete User</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>%%FNAME%%</td>
            <td>%%LNAME%%</td>
            <td>
                <button class="btnDelete">&times;</button>
            </td>
        </tr>
        <tr>
            <td>%%FNAME%%</td>
            <td>%%LNAME%%</td>
            <td>
                <button class="btnDelete">&times;</button>
            </td>
        </tr>
    </tbody>
</table>
&#13;
&#13;
&#13; http://jsfiddle.net/4pvrtghu/2/