2017-05-29 204 views
-2

似乎無法使複選框部分正常工作。如果勾選複選框,隱藏/顯示課程內容

.hide()工作正常,但else拒絕再次顯示它。

相同的代碼弄亂腳本的其他位在頁面上的其他版本,並且切換是一個沒有去,由於加載時間(如果訪問者點擊太早它結束了逆轉!)

從本質上講,我要查找的所有內容都是在勾選複選框時隱藏內容,如果未勾選則再次顯示內容。

jQuery(document).ready(function($) { 

    $('.sponsor_table').hide(); 
    $('.sponsor_address').hide(); 
    $('input[type="checkbox"]').click(function(){ 
     if($('input[name="remain_anonymous"]:checked')) { 
      $('.sponsor_name').hide(); 
      $('.sponsor_logo').hide(); 
      $('.sponsor_website').hide(); 
     } else { 
      $('.sponsor_name').show(); 
      $('.sponsor_logo').show(); 
      $('.sponsor_website').show(); 
     } 
    }); 
    $('input[type="radio"]').click(function(){ 
     if($('input[name="cb_sponsor_attendance"]:checked').val() == "Yes"){ 
      $('.sponsor_table').fadeIn('slow'); 
      $('.sponsor_address').hide(); 
     } 
     else if($('input[name="cb_sponsor_attendance"]:checked').val() == "No"){ 
      $('.sponsor_address').fadeIn('slow'); 
      $('.sponsor_table').hide(); 
     } 
    }); 

}); 

的複選框節的完整的HTML是:

<div id="remain_anonymous-wrap" class="form-row anonymous_sponsor">  <label class="give-label" for="ffm-remain_anonymous"> 
     Make this donation anonymous?      <span class="give-tooltip give-icon give-icon-question" 
       data-tooltip="Details will not be shared on the site, or at the event"></span> 
       </label> 
      <span data-required="no" data-type="checkbox"></span> 

    <span class="ffm-fields"> 

       <label> 
        <input type="checkbox" name="remain_anonymous[]" 
          value="Yes, please don&#039;t share my identity" /> 
        Yes, please don't share my identity     </label> 
          </span> 


    </div><div id="cb_sponsor_name-wrap" class="form-row sponsor_name">  <label class="give-label" for="ffm-cb_sponsor_name"> 
     Your Name/Business Name      <span class="give-tooltip give-icon give-icon-question" 
       data-tooltip="This will be shared both online and on the table at the gala."></span> 
       </label> 
      <input class="textfield" 
      id="ffm-cb_sponsor_name" type="text" 
      data-required="no" 
      data-type="text" name="cb_sponsor_name" 
      placeholder="" value="" 
      size=" 100" /> 

    </div><div id="cb_sponsor_logo-wrap" class="form-row sponsor_logo">  <label class="give-label" for="ffm-cb_sponsor_logo"> 
     Photo/Logo     </label> 

    <div id="ffm-cb_sponsor_logo-upload-container"> 
     <div class="ffm-attachment-upload-filelist"> 
      <a id="ffm-cb_sponsor_logo-pickfiles" class="button file-selector" 
       href="#">Select File(s)</a> 

      <span class="ffm-file-validation" data-required="no" data-type="file"></span> 
      <ul class="ffm-attachment-list give-thumbnails"> 
           </ul> 
     </div> 
    </div><!-- .container --> 


    <script type="text/javascript"> 
     jQuery(function ($) { 
      new Give_FFM_Uploader('ffm-cb_sponsor_logo-pickfiles', 'ffm-cb_sponsor_logo-upload-container', 1, 'cb_sponsor_logo', 'jpg,jpeg,gif,png,bmp,', 2048); 
     }); 
    </script> 
    </div><div id="cb_sponsor_website-wrap" class="form-row sponsor_website">  <label class="give-label" for="ffm-cb_sponsor_website"> 
     Website     </label> 

    <input id="ffm-cb_sponsor_website" type="url" class="give-url" 
      data-required="no" data-type="text"    name="cb_sponsor_website" 
      placeholder="" value="" 
      size="100"/> 

    </div> 
+2

也請告訴我們了'HTML'。 – Ionut

+1

[使用jQuery顯示/隱藏複選框]可能的副本(https://stackoverflow.com/questions/15766875/show-hide-with-checkbox-using-jquery) – urbz

+0

@urbz - 頁面上有另一個複選框,但放在那裏的插件,而不是我。我不知道他們使用的代碼是什麼,所有的HTML都是由插件生成的。我已經在這裏嘗試了其他六篇文章的建議,如果我已經嘗試了所有我能想到/發現的東西,我不覺得我應該被標記爲重複文章嗎? :/ –

回答

0

試試這個下面的代碼,並檢查:

$('input[type="checkbox"]').click(function() { 
 
     $('.sponsor_name,.sponsor_website,.sponsor_logo').show(); 
 
     if ($(this).is(':checked')) { 
 
     $('.sponsor_name,.sponsor_website,.sponsor_logo').hide(); 
 
     } 
 
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="remain_anonymous-wrap" class="form-row anonymous_sponsor">  <label class="give-label" for="ffm-remain_anonymous"> 
 
     Make this donation anonymous?      <span class="give-tooltip give-icon give-icon-question" 
 
       data-tooltip="Details will not be shared on the site, or at the event"></span> 
 
       </label> 
 
      <span data-required="no" data-type="checkbox"></span> 
 

 
    <span class="ffm-fields"> 
 

 
       <label> 
 
        <input type="checkbox" name="remain_anonymous[]" 
 
          value="Yes, please don&#039;t share my identity" /> 
 
        Yes, please don't share my identity     </label> 
 
          </span> 
 

 

 
    </div><div id="cb_sponsor_name-wrap" class="form-row sponsor_name">  <label class="give-label" for="ffm-cb_sponsor_name"> 
 
     Your Name/Business Name      <span class="give-tooltip give-icon give-icon-question" 
 
       data-tooltip="This will be shared both online and on the table at the gala."></span> 
 
       </label> 
 
      <input class="textfield" 
 
      id="ffm-cb_sponsor_name" type="text" 
 
      data-required="no" 
 
      data-type="text" name="cb_sponsor_name" 
 
      placeholder="" value="" 
 
      size=" 100" /> 
 

 
    </div><div id="cb_sponsor_logo-wrap" class="form-row sponsor_logo">  <label class="give-label" for="ffm-cb_sponsor_logo"> 
 
     Photo/Logo     </label> 
 

 
    <div id="ffm-cb_sponsor_logo-upload-container"> 
 
     <div class="ffm-attachment-upload-filelist"> 
 
      <a id="ffm-cb_sponsor_logo-pickfiles" class="button file-selector" 
 
       href="#">Select File(s)</a> 
 

 
      <span class="ffm-file-validation" data-required="no" data-type="file"></span> 
 
      <ul class="ffm-attachment-list give-thumbnails"> 
 
           </ul> 
 
     </div> 
 
    </div><!-- .container --> 
 

 
    </div><div id="cb_sponsor_website-wrap" class="form-row sponsor_website">  <label class="give-label" for="ffm-cb_sponsor_website"> 
 
     Website     </label> 
 

 
    <input id="ffm-cb_sponsor_website" type="url" class="give-url" 
 
      data-required="no" data-type="text"    name="cb_sponsor_website" 
 
      placeholder="" value="" 
 
      size="100"/> 
 

 
    </div>

+0

Nope :(現在不做任何事情,點擊它沒有任何效果 –

+0

已更新。希望這會有所幫助 –

+0

唉,該腳本已包含在頁面中,並且正在處理其他元素。插件。我無法控制HTML輸出,並且無法粘貼整個東西,因爲它太長了......例如,Web地址是:[link](https://project365.site/support/sydney-2017-table -2 /) –

0

嘗試使用此功能時,您複選框

$("input[name=anonymous_sponsor]").change(function() { 
     if(this.checked) { 
     $('.sponsor_name').hide(); 
     $('.sponsor_logo').hide(); 
     $('.sponsor_website').hide(); 
    } else { 
     $('.sponsor_name').show(); 
     $('.sponsor_logo').show(); 
     $('.sponsor_website').show(); 
    } 

    }); 
+0

之前試過了這個版本,完全沒有做任何事情:( –

+0

試試我更新後的ans @JonWatson – Jay

+0

唉,仍然沒有做任何事情...檢查或取消選中該框沒有任何效果 –

0

假設你有,你要聽變革

$('input[name="your_input_field_name"]').change(function(){ 
    if($(this).is(':checked')){ 
    //if ture 
    }else{ 
    //if false 
    } 
}); 
+0

都能跟得上:('\t \t \t \t \t \t $(' 輸入[名稱= 「remain_anonymous」] ')改變(函數(){ \t \t \t \t \t \t \t如果($(本)。是(':檢查 ')){ \t \t \t \t \t \t \t $(' SPONSOR_NAME ')隱藏(); \t \t \t \t \t $('。sponsor_logo')隱藏(); \t \t \t \t \t $('。sponsor_website')。hide(); \t \t \t \t \t \t \t}其他{ \t \t \t \t \t \t \t $( 'SPONSOR_NAME。')顯示()。 \t \t \t \t \t $('。sponsor_logo')。show(); \t \t \t \t \t $('。sponsor_website')。show(); \t \t \t \t \t \t} \t \t \t \t \t \t});」什麼都不做。 –

+0

Bah!輸入名稱必須包括方括號!什麼?!將它更改爲remain_anonymous []似乎已經成功了! –

+0

您已使用輸入數組。是否有許多remaining_anonymous的實例?如果沒有擺脫名稱末尾的括號 – Lasithds

0

解決的名稱「your_input_field_name」 <input name="your_input_field_name" type="checkbox" >一個輸入字段:

顯然,輸入名稱必須與HTML完全匹配此正常工作,包括未在WordPress後端列出的方括號(截圖如下)。

a back_end

改變輸入名稱:remain_anonymous []似乎已經完成了帽子戲法。儘管頁面上只有一個remaining_anonymous實例,但如果沒有它們,它將無法正常工作。

我不知道爲什麼,但它的工作。

謝謝大家的幫助,並感謝ionut建議我發佈HTML。我第一次看到它... doh

0

試試這個,我修改了你的代碼。

jQuery(document).ready(function($) { 
 

 
    $('.sponsor_table').hide(); 
 
    $('.sponsor_address').hide(); 
 
    $('input[name="remain_anonymous[]"]').click(function(evt){ 
 
     if($(evt.currentTarget).is(':checked')) { 
 
      $('.sponsor_name').hide(); 
 
      $('.sponsor_logo').hide(); 
 
      $('.sponsor_website').hide(); 
 
     } else { 
 
      $('.sponsor_name').show(); 
 
      $('.sponsor_logo').show(); 
 
      $('.sponsor_website').show(); 
 
     } 
 
    }); 
 
    $('input[type="radio"]').click(function(){ 
 
     if($('input[name="cb_sponsor_attendance"]:checked').val() == "Yes"){ 
 
      $('.sponsor_table').fadeIn('slow'); 
 
      $('.sponsor_address').hide(); 
 
     } 
 
     else if($('input[name="cb_sponsor_attendance"]:checked').val() == "No"){ 
 
      $('.sponsor_address').fadeIn('slow'); 
 
      $('.sponsor_table').hide(); 
 
     } 
 
    }); 
 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="remain_anonymous-wrap" class="form-row anonymous_sponsor">  <label class="give-label" for="ffm-remain_anonymous"> 
 
     Make this donation anonymous?      <span class="give-tooltip give-icon give-icon-question" 
 
       data-tooltip="Details will not be shared on the site, or at the event"></span> 
 
       </label> 
 
      <span data-required="no" data-type="checkbox"></span> 
 

 
    <span class="ffm-fields"> 
 

 
       <label> 
 
        <input type="checkbox" name="remain_anonymous[]" 
 
          value="Yes, please don&#039;t share my identity" /> 
 
        Yes, please don't share my identity     </label> 
 
          </span> 
 

 

 
    </div><div id="cb_sponsor_name-wrap" class="form-row sponsor_name">  <label class="give-label" for="ffm-cb_sponsor_name"> 
 
     Your Name/Business Name      <span class="give-tooltip give-icon give-icon-question" 
 
       data-tooltip="This will be shared both online and on the table at the gala."></span> 
 
       </label> 
 
      <input class="textfield" 
 
      id="ffm-cb_sponsor_name" type="text" 
 
      data-required="no" 
 
      data-type="text" name="cb_sponsor_name" 
 
      placeholder="" value="" 
 
      size=" 100" /> 
 

 
    </div><div id="cb_sponsor_logo-wrap" class="form-row sponsor_logo">  <label class="give-label" for="ffm-cb_sponsor_logo"> 
 
     Photo/Logo     </label> 
 

 
    <div id="ffm-cb_sponsor_logo-upload-container"> 
 
     <div class="ffm-attachment-upload-filelist"> 
 
      <a id="ffm-cb_sponsor_logo-pickfiles" class="button file-selector" 
 
       href="#">Select File(s)</a> 
 

 
      <span class="ffm-file-validation" data-required="no" data-type="file"></span> 
 
      <ul class="ffm-attachment-list give-thumbnails"> 
 
           </ul> 
 
     </div> 
 
    </div><!-- .container --> 
 

 

 
    <script type="text/javascript"> 
 
     jQuery(function ($) { 
 
      //new Give_FFM_Uploader('ffm-cb_sponsor_logo-pickfiles', 'ffm-cb_sponsor_logo-upload-container', 1, 'cb_sponsor_logo', 'jpg,jpeg,gif,png,bmp,', 2048); 
 
     }); 
 
    </script> 
 
    </div><div id="cb_sponsor_website-wrap" class="form-row sponsor_website">  <label class="give-label" for="ffm-cb_sponsor_website"> 
 
     Website     </label> 
 

 
    <input id="ffm-cb_sponsor_website" type="url" class="give-url" 
 
      data-required="no" data-type="text"    name="cb_sponsor_website" 
 
      placeholder="" value="" 
 
      size="100"/> 
 

 
    </div>

+0

有效,但會影響頁面上的所有複選框。每次點擊它們時,都會顯示/隱藏行爲。因此,如果您勾選了「keep_anonymous」,然後決定要支付交易費用或接受條款和條件,則您將隱藏的字段再次顯示。 –

+0

我已經將jquery選擇器更改爲name =「remain_anonymous []」,您在html中使用該選擇器。希望有所幫助。請參閱答案編輯 – masif

相關問題