JQuery .remove()正在删除所有标签跨度,如何修复?

时间:2016-12-02 23:13:30

标签: jquery

我正在编写一个函数,当一个值被添加到隐藏输入(图像id)时,它是可见标签的子元素,包含警告文本的范围会消失。好吧,我得到了警告文本消失,但它消失在我的所有表单元素上。提前感谢您的所有帮助和意见!这是我正在处理的事情:

更改前的HTML:

        <div class="form-group-horizontal">
        <div class="form-group ellipsis">
          <label class="media-library ellipsis">
            <label for="before-image-modal-trigger" class="generic-form__label"><a class="clear-image">Clear Image</a>Before Image</label>
            <input type="hidden" name="before_image" id="before-image" class="invisible" accept="image/jpeg,image/jpg,image/x-png,image/png,.jpg,.jpeg,.png" data-validation-invalid="Invalid filetype. The supported file types are: .jpg, and .png" data-validation-required="The media field is required.">
            <button class="button modal-trigger" data-modal="media-library-modal" id="before-image-modal-trigger"><i class="fa fa-plus"></i>Select</button>
            <span class="selected-image">Select a Before image file.</span>
          </label>
        </div>
        <div class="form-group ellipsis">
          <label class="media-library ellipsis">
            <label for="after-image-modal-trigger" class="generic-form__label">After Image</label>
            <input type="hidden" name="after_image" id="after-image" class="invisible" accept="image/jpeg,image/jpg,image/x-png,image/png,.jpg,.jpeg,.png" data-validation-invalid="Invalid filetype. The supported file types are: .jpg, and .png" data-validation-required="The media field is required.">
            <button class="button modal-trigger" data-modal="media-library-modal" id="after-image-modal-trigger"><i class="fa fa-plus"></i>Select</button>
            <span class="selected-image">Select an After image file.</span>
          </label>
        </div>
      </div>

      <div class="form-group">
        <label for="title" class="generic-form__label">Title</label>
        <input type="text" name="title" id="title" class="generic-form__input" data-validation-required="The title field is required." />
      </div>

更改后的HTML:

        <div class="form-group-horizontal">
        <div class="form-group ellipsis">
          <label class="media-library ellipsis">
            <label for="before-image-modal-trigger" class="generic-form__label"> <span class="text text-danger error">The media field is required.</span> <a class="clear-image">Clear Image</a>Before Image</label>
            <input type="hidden" name="before_image" id="before-image" class="invisible" accept="image/jpeg,image/jpg,image/x-png,image/png,.jpg,.jpeg,.png" data-validation-invalid="Invalid filetype. The supported file types are: .jpg, and .png" data-validation-required="The media field is required.">
            <button class="button modal-trigger" data-modal="media-library-modal" id="before-image-modal-trigger"><i class="fa fa-plus"></i>Select</button>
            <span class="selected-image">Select a Before image file.</span>
          </label>
        </div>
        <div class="form-group ellipsis">
          <label class="media-library ellipsis">
            <label for="after-image-modal-trigger" class="generic-form__label"> <span class="text text-danger error">The media field is required.</span> After Image</label>
            <input type="hidden" name="after_image" id="after-image" class="invisible" accept="image/jpeg,image/jpg,image/x-png,image/png,.jpg,.jpeg,.png" data-validation-invalid="Invalid filetype. The supported file types are: .jpg, and .png" data-validation-required="The media field is required.">
            <button class="button modal-trigger" data-modal="media-library-modal" id="after-image-modal-trigger"><i class="fa fa-plus"></i>Select</button>
            <span class="selected-image">Select an After image file.</span>
          </label>
        </div>
      </div>

      <div class="form-group">
        <label for="title" class="generic-form__label">Title </label>
        <input type="text" name="title" id="title" class="generic-form__input" data-validation-required="The title field is required." />
      </div>

JQuery的: 在函数上有一个.trigger('change'),它将图像id添加到隐藏输入中的值btw。那是另一个文件。

if (field.is('input[type="hidden"][data-validation-required]') && $('label > span.error')) {
    $(this).on('change', function(){
      var value = $(this).val();

      if (value.length){
          $('label > span.error').remove();
      }
    });
  }

我还试图看看if(value.length&gt; 1)是否执行了删除操作,但是还删除了所有内容中的警告文本。

1 个答案:

答案 0 :(得分:0)

在伪代码$('label > span.error').remove()中说'找到然后删除所有 span,其中error类是label'的直接子项。

您要删除span.error的{​​{1}}的子项label,其中input已被更改:{/ p>

$(this).parent('label').find('span.error').remove():