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

时间:2019-07-23 12:03:19

标签: javascript jquery radio-button

我有一个单选按钮,其值为Yes和No。 如果单击“是”,则需要启用2个输入字段。如果我尝试在不输入值的情况下保存它们,则会显示验证消息。并且在显示验证消息后不久,如果我尝试单击“否”,则必须禁用输入字段,并且应删除验证消息。

当我单击“否”(“否”的值为0)时,字段被禁用,但是验证消息仍然存在。

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:

<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>

我还有其他几个字段,其验证消息显示在每个元素下同一span类中。因此,如果单击“否”,那么我只需要删除上述两个字段的验证消息。

1 个答案:

答案 0 :(得分:0)

之所以不起作用,是因为您使用的find方法正在查找作为所选输入元素的子元素的元素,并且span和input元素是彼此的兄弟姐妹。

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

您可以使用元素+元素选择器来选择span元素:

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

在这种情况下,它将获得一个范围,该范围在ID为license_number的元素之后

CSS element+element Selector

相关问题