如何删除动态父级 通过子的OnClick
我通过 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()
函数可以删除父标签,而不会意外删除所有
标签?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web
技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的
隐私政策
了解更多相关信息。 单击 接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文
我通过 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()
函数可以删除父标签,而不会意外删除所有 标签?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的
隐私政策
了解更多相关信息。 单击
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文
发布评论
评论(2)
考虑到这个是删除按钮:
以下内容即可:
我建议您使用 jQuery 事件处理程序,而不是使用内联
onclick
等。$(this)
是被单击按钮的 jQuery 对象,.closest()
将在按钮的父级中查找并找到第一个tr< /code>,然后将其删除。
jsFiddle by @ShadowWizard
最好的方法是更改删除按钮的 HTML:
这样您就可以定位您的删除按钮如下所示:
这更好,因为在前面的示例中,表中每个可能的
input type="button"
都会获得该事件,即使我们只需要这些特殊的事件(而不是如果表中没有其他按钮,则会出现问题)。Considering this one is the remove button:
The following will do:
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 firsttr
, then remove it.jsFiddle by @ShadowWizard
The best way would be to change the HTML for your remove button:
so you can target your remove button like this:
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).bazmegakapa 的答案应该可以解决问题。另外,你真的应该避免使用内联 Javascript,这通常是不好的做法。相反,
为了遵循 jQuery 的约定,使用元素对象的正确范围,您可以这样做:
然后在 AddTR 函数中,您可以简单地将元素表引用为
this
它使事情保持可预测性并遵循相同的约定。
等一下......
理论上,尽管
AddTR()
函数正在添加一个表行,但这有点误导,因为它所做的只是将一个元素附加到该上下文。你真正想做的就是函数所说的; 添加一个表格行!而不是你的做法这是相当难看的,并且如果你更改表格行结构,将会导致一些变化。相反,使用
.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:
Also to keep up with the convention of jQuery using the correct scope of the element object, you could do:
Then in your AddTR function you can simply reference the element table as
this
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 doingWhich is rather ugly and will cause some changes if you change your table row structures. Instead, use
.clone
to help you:See fiddle: http://jsfiddle.net/garreh/6NUK3/1/