繁体   English   中英

如何在 Jquery 中删除标签

[英]How to remove label in Jquery

我在 Jquery 中添加了一个标签

 $('[id$="refAprtyAppId"]').after('<label class="error" id="refAprtyAppIdError">Error: Referral Id is required.</label>');

我努力了

$('[id$="refAprtyAppId"]').parent().find("label#refAprtyAppIdError").remove();

删除标签,但删除失败。

html是

 <div class="area">
   <input id="refAprtyAppId" value="" styleClass="externalAppId referralId"/>
 </div>

这里有什么问题?

为什么不使用 id 选择器。 你在那里有一个id。 用它。

 $('#refAprtyAppIdError').remove();

根据 html 文档,您可以在同一页面上拥有多个同名的 id。 如果您有多个“错误标签”,则可以使用 -

$('label.error').remove();

添加答案以更好地阐明我的评论。

您的代码工作正常:

 $('[id$="refAprtyAppId"]').after('<label class="error" id="refAprtyAppIdError">Error: Referral Id is required.</label>'); $('#remove').on('click', function() { $('[id$="refAprtyAppId"]').parent().find("label#refAprtyAppIdError").remove(); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="area"> <input id="refAprtyAppId" value="" styleClass="externalAppId referralId" /> </div> <button type='button' id='remove'>Remove Label</button>

检查浏览器中的 JavaScript 控制台是否有其他错误。 可能是 jQuery 没有被正确引用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM