2016-12-02 37 views
0

我正在寫一個函數,當一個值被添加到一個隱藏的輸入(圖像ID),這是一個可見標籤的孩子,包含警告文本的跨度消失。那麼,我得到了警告文本消失,但它消失在我所有的表單元素。預先感謝您的幫助和投入!以下是我與處理:JQuery .remove()將刪除所有標籤跨度,如何解決?

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( '變化')圖像ID到隱藏輸入的值,順便說一句。這是另一個文件。

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> 1)是否執行刪除操作,但是也刪除了所有內容的警告文本。

+1

'$( '標籤> span.error')刪除()'這個說「ALL刪除span.error是一個標籤的孩子'。您想要刪除span.error,該span.error是此輸入已更改的標籤的子項。也許'$(this).parent('label')。find('span.error')。remove()'。 – Moob

+0

完全做到了!謝謝! – knitalittlekitty

+0

很高興我能幫到你。我已發佈我的評論作爲實際答案,爲將來訪問者提供此問題的好處。 – Moob

回答

0

在僞代碼$('label > span.error').remove()說:「找到然後刪除所有span具有類error這是一個label的直接孩子。

要刪除span.error是在這個input被更改label的孩子:

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