如何删除动态父级 通过子的OnClick

发布于 2024-11-07 03:02:48 字数 1077 浏览 10 评论 0 原文

我通过 Javascript var 添加

var txtBox = "<tr id='dynTR'><td><input type='text' class='textBoxes' /></td><td><input type='text' class='textBoxes' value='0' /></td><td><input type='button' value='-' /></td></tr>";

我的函数是:

function AddTR(table) {
            $(table).append(txtBox);
        }

HTML 中的我的表结构(以及函数的按钮)是:

<table id="tblTest" class="testTable">
            <thead>
                <tr>
                    <td>Product</td>
                    <td>Quantity</td>
                    <td>Remove TR</td>
                </tr>
            </thead>
            <tbody>

            </tbody>
        </table>
        <br />
        <input type="Button" id="btnTest" value="Add Table Row" onclick="AddTR($('#tblTest'))" />

那么我该如何使用jQuery 中的 .remove() 函数可以删除父标签,而不会意外删除所有 标签?

I'm adding the <tr> via a Javascript var:

var txtBox = "<tr id='dynTR'><td><input type='text' class='textBoxes' /></td><td><input type='text' class='textBoxes' value='0' /></td><td><input type='button' value='-' /></td></tr>";

With my function being:

function AddTR(table) {
            $(table).append(txtBox);
        }

My table structure (along with the button for the function) in the HTML being:

<table id="tblTest" class="testTable">
            <thead>
                <tr>
                    <td>Product</td>
                    <td>Quantity</td>
                    <td>Remove TR</td>
                </tr>
            </thead>
            <tbody>

            </tbody>
        </table>
        <br />
        <input type="Button" id="btnTest" value="Add Table Row" onclick="AddTR($('#tblTest'))" />

So how do I go about using the .remove() function in jQuery to get rid of the parent tag without accidentally removing all <tr id='dynTR'> tags?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

他不在意 2024-11-14 03:02:49

考虑到这个是删除按钮:

<input type='button' value='-' />

以下内容即可:

$('#tblTest input[type="button"]').click(function () {
     $(this).closest('tr').remove();
});

我建议您使用 jQuery 事件处理程序,而不是使用内联 onclick 等。 $(this) 是被单击按钮的 jQuery 对象,.closest() 将在按钮的父级中查找并找到第一个 tr< /code>,然后将其删除。

jsFiddle by @ShadowWizard

最好的方法是更改​​删除按钮的 HTML:

<input type='button' value='-' class='removeButton' />

这样您就可以定位您的删除按钮如下所示:

$('#tblTest .removeButton').click(...

这更好,因为在前面的示例中,表中每个可能的 input type="button" 都会获得该事件,即使我们只需要这些特殊的事件(而不是如果表中没有其他按钮,则会出现问题)。

Considering this one is the remove button:

<input type='button' value='-' />

The following will do:

$('#tblTest input[type="button"]').click(function () {
     $(this).closest('tr').remove();
});

I'd suggest you use jQuery event handlers instead of using inline onclick and friends. $(this) is the jQuery object of the button that was clicked, .closest() will look in the parents of the button and find the first tr, then remove it.

jsFiddle by @ShadowWizard

The best way would be to change the HTML for your remove button:

<input type='button' value='-' class='removeButton' />

so you can target your remove button like this:

$('#tblTest .removeButton').click(...

This is better because with the previous example, every possible input type="button" in the table would get the event, even though we just need it on these special ones (not a problem if there are no other buttons in the table).

ぽ尐不点ル 2024-11-14 03:02:49

bazmegakapa 的答案应该可以解决问题。另外,你真的应该避免使用内联 Javascript,这通常是不好的做法。相反,

$('#btnTest').click(function() { AddTR($('#tblTest')); });

为了遵循 jQuery 的约定,使用元素对象的正确范围,您可以这样做:

$('#btnTest').click(function() { AddTR.call($('#tblTest')[0]); });

然后在 AddTR 函数中,您可以简单地将元素表引用为 this

function AddTR() {
    $(this).append(txtBox);
}

它使事情保持可预测性并遵循相同的约定。


等一下......

理论上,尽管 AddTR() 函数正在添加一个表行,但这有点误导,因为它所做的只是将一个元素附加到该上下文。你真正想做的就是函数所说的; 添加一个表格行!而不是你的做法

var txtBox = "<tr id='dynTR'><td><input type='text' class='textBoxes' /></td><td><input type='text' class='textBoxes' value='0' /></td><td><input type='button' value='-' /></td></tr>";

这是相当难看的,并且如果你更改表格行结构,将会导致一些变化。相反,使用 .clone 来帮助您:

function AddTR() {
   $(this).append($('tr:last').clone());
}

参见 fiddle: http://jsfiddle.net/garreh/6NUK3/1/

bazmegakapa answer should do the trick. Also you should really avoid using inline Javascript, it's generally bad practise. Instead do:

$('#btnTest').click(function() { AddTR($('#tblTest')); });

Also to keep up with the convention of jQuery using the correct scope of the element object, you could do:

$('#btnTest').click(function() { AddTR.call($('#tblTest')[0]); });

Then in your AddTR function you can simply reference the element table as this

function AddTR() {
    $(this).append(txtBox);
}

It keeps things predictable and follows the same convention.


Hang on a minute....

In theory although the AddTR() function is adding a table row, it's a bit misleading because all it's doing is just appending an element to that context. What you really want to do is just what the function says; add a table row! instead your doing

var txtBox = "<tr id='dynTR'><td><input type='text' class='textBoxes' /></td><td><input type='text' class='textBoxes' value='0' /></td><td><input type='button' value='-' /></td></tr>";

Which is rather ugly and will cause some changes if you change your table row structures. Instead, use .clone to help you:

function AddTR() {
   $(this).append($('tr:last').clone());
}

See fiddle: http://jsfiddle.net/garreh/6NUK3/1/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文