简体   繁体   English

清除特定输入字段的跨度文本

[英]Clear a span text of a particular input field

I have a radio button whose values are Yes and No. If I click on Yes, I need to enable 2 input fields. 我有一个单选按钮,其值为Yes和No。如果单击Yes,则需要启用2个输入字段。 If I try to save them without entering values it shows validation messages. 如果我尝试在不输入值的情况下保存它们,则会显示验证消息。 And soon after it shows validation message , if I try to click on No, then input fields must be disable and should remove validation messages. 并且在显示验证消息后不久,如果我尝试单击“否”,则必须禁用输入字段,并且应删除验证消息。

When I click on No (value of No is 0), fields are getting disabled, but validation message remains there. 当我单击“否”(“否”的值为0)时,字段将被禁用,但验证消息仍保留在那里。

Js: JS:

if($("input[name=is_emt_license_available]:checked").val() == 0) {
  $("#license_number").attr("disabled", "disbled");  
  $("#license_issued_body").attr("disabled", "disabled");
  $('#license_number').find('span').html('');
  $('#license_issued_body').find('span').html(''); 
} 

Html: HTML:

<input class="form-control mb-0" name="license_number" id="license_number" placeholder="ENTER LICENSE NUMBER" value="" disabled="disabled">

<span class="error" style="color:#e03b3b">Enter License Number.</span>

<input class="form-control mb-0" name="license_issued_body" id="license_issued_body" placeholder="ENTER LICENSE ISSUING BOARD" value="" disabled="disabled">

 <span class="error" style="color:#e03b3b">Enter License ISSUING BOARD.</span>

I have several other fields whose validation messages are shown inside the same span class under each element. 我还有其他几个字段,其验证消息显示在每个元素下的同一span类中。 So if I click on No, then I have to remove only the validation messages of the above two fields. 因此,如果单击“否”,那么我只需要删除上述两个字段的验证消息。

It is not working because the find method you are using is looking for elements which are children of the selected input element, and the span and input elements are siblings of each other. 它不起作用,因为您正在使用的find方法正在查找作为所选输入元素的子级的元素,并且span和input元素是彼此的同级。

https://api.jquery.com/find/ https://api.jquery.com/find/

You can use the element + element selector to select the span element: 您可以使用element + element选择器来选择span元素:

$('#license_number + span').html('');

In this case it will get a span that follows an element with an id of license_number 在这种情况下,它将获得一个跨度,该跨度跟随一个具有license_number id的元素

CSS element+element Selector CSS元素+元素选择器

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

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