[英]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.