选中复选框时如何使错误消息消失

How to make error message disappear when Checkbox is Checked

编辑:

<div class="editor-item">
<label for="Location">Location</label><input class="input-validation-error ui-autocomplete-input" data-jqui-acomp-delay="400" data-jqui-acomp-hiddenvalue="LocationId" data-jqui-acomp-minlength="3" data-jqui-acomp-source="/Loc/Search" data-jqui-type="autocomplete" id="Location" name="Location" type="text" value="123 wrong address" autocomplete="off">
<span role="status" aria-live="polite" class="ui-helper-hidden-accessible"></span><span class="field-validation-error" data-valmsg-for="Location" data-valmsg-replace="true">Location not found</span></div>

当用户提交按钮时,我会检查该位置是否存在,如果不存在,我会添加 ModelState.AddmodelError

ModelState.AddModelError((LocationViewModel m) => m.Location, "Location not found");

我的问题是:当用户点击复选框时Create Location如何让错误消失?

 <span id="CreateNLocation"   >
    @Html.LabelFor(model => model.CreateLocation, "Create Location")
    @Html.CheckBoxFor(model => model.CreateLocation)
 </span>

在 运行 时间呈现:

<span id="CreateNLocation" style="">
   <label for="CreateLocation">Create Location</label>
   <input data-val="true" data-val-required="The Create new Location field is required." id="CreateLocation" name="CreateLocation" type="checkbox" value="true">

$('#CreateLocation').click(function() {
    $('.field-validation-error).remove();
});

如果我没有正确理解你的问题,你只是想在选中复选框时让错误消失?如果是这样,这应该适合您。

编辑:误读了问题,我需要错误消息的标记(div,跨度,无论是什么)。无论元素是什么,将 .remove() 选择器替换为正确的选择器。

$('#CreateLocation').click(function() {
    $(this).parent(wrapper).find('.field-validation-error').remove();
});

根据您对问题的评论

$('#CreateLocation').change(function(){
  if($(this).prop("checked")) {
    $("span[data-valmsg-for='Location']").hide();
  } else {
    $("span[data-valmsg-for='Location']").show();
  }
});

a fiddle

这是一个 JS Fiddle 示例,如果您想 show/hide 它基于复选框是否被选中。

$('#CreateLocation').click(function() {
   var $checkbox = $('#CreateLocation');
   var $errMsg   = $(this).parents('#parent-example')
                        .find('.field-validation-error');
   $checkbox.prop('checked') ? $errMsg.hide() : $errMsg.show();
});

http://jsfiddle.net/ga7kpn3r/1/