2016-07-07 6 views
0

Wordpressのページがあり、誰かがチェックボックスをクリックすると2つの入力フィールドを表示および非表示にするjQueryを実行しています。問題は、このコードは私のホームページ上で完璧に動作しますが、サブページ上で同じことを(同じHTML構造& jqueryを使用して)実行しようとすると、もう機能しなくなります。それは、jQueryファイルの1つの部分がそのページでうまく動作するため、ファイルがロードされないということではなく、ファイルの最後にconsole.logがあり、ファイルが完全にロードされたことを示します。jQueryは特定のWordpressページでは実行されません

www.yogagids.nlはホームページで、「Evenementen」チェックボックスをオンにすると2つの日付ピッカーフィールドが表示され、選択を解除すると非表示になります。どのように動作するはずです。

www.yogagids.nl/listingsこれは動作しないページですか。同じ話ですが、 "evenementen"チェックボックスをクリックすると、2つのdatepickerフィールドが表示され、非表示になります。しかし、このページでは何も起こりません。

私は本当にここに迷っています。どんな助けでも大歓迎です。

ここにこのページで実行されるすべてのコードがありますが、実際のファイルは大きくなりますが、その多くの文字を投稿できませんでした。

jQuery(document).ready(function() { 

     //Create dummy input fields (altField) to show the user, this way the user sees a proper dateFormat while saving Unix Timestamp in the database. 
     //Do this for both the Add Listing page and the Search Fields 
     jQuery("#event_date_start, #filter_event_date_start").after('<input type="text" class="input-text" name="event_date_start_dummy" id="event_date_start_dummy" placeholder="Begindatum"></input>'); 
     jQuery("#event_date_end, #filter_event_date_end").after('<input type="text" class="input-text" name="event_date_end_dummy" id="event_date_end_dummy" placeholder="Einddatum"></input>'); 

     //Define Dutch format and naming for the calendars, set the calendar to use unix date (easier to compare), set an alternative field to show the proper format. 
     jQuery("#event_date_start, #filter_event_date_start").datepicker({ 
      dateFormat: "@", 
      altFormat: "dd-mm-yy", 
      altField: "#event_date_start_dummy", 
      minDate: 0, 
      dayNames: [ "Zondag", "Maandag", "Dinsdag", "Woensdag", "Donderdag", "Vrijdag", "Zaterdag" ], 
      dayNamesMin: [ "Zo", "Ma", "Di", "Wo", "Do", "Vr", "Za" ], 
      monthNames: [ "januari", "februari", "maart", "april", "mei", "juni", "juli", "augustus", "september", "oktober", "november", "december" ], 
      monthNamesShort: [ "jan", "feb", "maa", "apr", "mei", "jun", "jul", "aug", "sep", "okt", "nov", "dec" ], 
      changeMonth: true, 
      changeYear: true 
     }); 


     //Define Dutch format and naming for the calendars, set the calendar to use unix date (easier to compare), set an alternative field to show the proper format. 
     jQuery("#event_date_end, #filter_event_date_end").datepicker({ 
      dateFormat: "@", 
      altFormat: "dd-mm-yy", 
      altField: "#event_date_end_dummy", 
      minDate: 0, 
      dayNames: [ "Zondag", "Maandag", "Dinsdag", "Woensdag", "Donderdag", "Vrijdag", "Zaterdag" ], 
      dayNamesMin: [ "Zo", "Ma", "Di", "Wo", "Do", "Vr", "Za" ], 
      monthNames: [ "januari", "februari", "maart", "april", "mei", "juni", "juli", "augustus", "september", "oktober", "november", "december" ], 
      monthNamesShort: [ "jan", "feb", "maa", "apr", "mei", "jun", "jul", "aug", "sep", "okt", "nov", "dec" ], 
      changeMonth: true, 
      changeYear: true 
     }); 


     //Whenever a user selects a start date, set the minimum end date to the start date. 
     //This prevents users from selecting end dates that happen before the start date. 
     jQuery('#event_date_start, #filter_event_date_start').on('change', function() { 
      // Get the Start Date 
      var minDate = jQuery("#event_date_start, #filter_event_date_start").val(); 
      // Set the minimum End Date 
      jQuery("#event_date_end, #filter_event_date_end").datepicker("option", "minDate", minDate); 
     }); 


     //Chosen.js is not used on mobile devices, so running this code will interrupt the javascript. 
     //By running it only if the screen is bigger than 600px we make sure this doesnt happen. 
     if (window.innerWidth <= 600) { 
      //Do nothing 
     } else { 
      //Category dropdown uses Chosen.js, a dropdown library. 
      //When on the map view page(/listings) or submit form(/sumbit-listing) but not on my account(/my-account/listings), prevent category dropdown from closing when selecting a category. 
      if (currentLocation.toLowerCase().indexOf("listings") >= 0 && document.location.href.indexOf('my-account') === -1 || currentLocation.toLowerCase().indexOf("submit-listing") >= 0){ 
       console.log('Chosen dropdown fix active'); 

       //Source of fix: https://github.com/harvesthq/chosen/issues/1546#issuecomment-63448852 
       //By user: ronanquillevere 
       var chosen = jQuery('.job-manager-category-dropdown').chosen().data('chosen'); 
       var autoClose = false; 
       var chosen_resultSelect_fn = chosen.result_select; 
       chosen.search_contains = true; 

       chosen.result_select = function(evt) { 
        var resultHighlight = null; 

        if(autoClose === false) 
        { 
         evt['metaKey'] = true; 
         evt['ctrlKey'] = true; 

         resultHighlight = chosen.result_highlight; 
        } 

        var stext = chosen.get_search_text(); 

        var result = chosen_resultSelect_fn.call(chosen, evt); 

        if(autoClose === false && resultHighlight !== null) 
         resultHighlight.addClass('result-selected'); 

        this.search_field.val(stext);    
        this.winnow_results(); 
        this.search_field_scale(); 

        return result; 
       } 
      }; 
     } 



     //Get multiple categories from the url, that were passed from home. 
     //Source: http://stackoverflow.com/questions/22209307/how-to-get-multiple-parameters-with-same-name-from-a-url-in-javascript 
     function getQueryParams(key) { 
      qs = location.search; 
      var params = []; 
      var tokens; 
      var re = /[?&]?([^=]+)=([^&]*)/g; 
      while (tokens = re.exec(qs)) 
      { 
       if (decodeURIComponent(tokens[1]) == key) 
       params.push(decodeURIComponent(tokens[2])); 
      } 
      return params; 
     } 
     var ms_cats = getQueryParams('ms_cat'); 

     //console.log(ms_cats); 

     //Set chosen.js values by using the values that were passed in the url. 
     //Source: http://stackoverflow.com/questions/8980131/changing-selection-in-a-select-with-the-chosen-plugin 
     jQuery('.job-manager-category-dropdown ').val(ms_cats).trigger('chosen:updated'); 



     jQuery(".listingTypeSelect input[value=event]").change(function() { 
      console.log('clicked'); 
      if (!jQuery(this).is(':checked')) { 

       jQuery('.search_event_date').css('display', 'none'); 
       jQuery(".listingTypeSelect input[value=studio]").prop('disabled', false); 
       jQuery(".listingTypeSelect input[value=teacher]").prop('disabled', false); 

       if (jQuery(window).width() > 992) { 
        jQuery('.search_home_row_2 > div').css('width', '33.333%'); 
       } else { 
        jQuery('.search_home_row_2 > div').css('width', '100%'); 
       } 


      } else { 

       jQuery('.search_event_date').css('display', 'inline-block'); 
       jQuery(".listingTypeSelect input[value=studio]").attr('checked', false); // Unchecks it 
       jQuery(".listingTypeSelect input[value=teacher]").attr('checked', false); // Unchecks it 
       jQuery(".listingTypeSelect input[value=studio]").prop('disabled', true); 
       jQuery(".listingTypeSelect input[value=teacher]").prop('disabled', true); 

       if (jQuery(window).width() > 992) { 
        jQuery('.search_home_row_2 > div').css('width', '23.333%'); 
        jQuery('.search_home_row_2 .search_event_date').css('width', '30%'); 
       } else { 
        jQuery('.search_home_row_2 > div').css('width', '100%'); 
        jQuery('.search_home_row_2 .search_event_date').css('width', '100%'); 
       } 

      } 
     }); 



     //Notify that the file has loaded without problems. 
     console.log("Finished loading listify-child.js!"); 
    }); 
+0

ローディングオーダーの問題のようです。そのチェックボックスは、コードが初めて実行されたときには検出されません。コンソール内で手動でjQueryを実行している場合( "listTypeSelect input [value = event]")、change(function(){(clicked ');});クリックハンドラがそのチェックボックスにアタッチされます。ページの生成方法はわかりませんが、右側のセクションがレンダリングされるコールバックにアクセスできる場合は、そのセクションを使用してこのスクリプトを呼び出す必要があります。この理論をテストするには、console.log(jQuery( "。listingTypeSelect input [value = event]"))を追加し、何かを返すかどうかを確認します。 – dmoo

答えて

0

変更イベントをバインドするときに、チェックボックスのHTML要素が存在しないか、対応する値/クラスが欠落していることはほぼ確実です。 on方法を使用して試してみても、親要素のイベントをキャプチャし、「イベント伝播」を活かし:

 jQuery(".listingTypeSelect").on('change', 'input[value=event]', function() { 
     console.log('clicked'); 
     if (!jQuery(this).is(':checked')) { 

      jQuery('.search_event_date').css('display', 'none'); 
      jQuery(".listingTypeSelect input[value=studio]").prop('disabled', false); 
      jQuery(".listingTypeSelect input[value=teacher]").prop('disabled', false); 

      if (jQuery(window).width() > 992) { 
       jQuery('.search_home_row_2 > div').css('width', '33.333%'); 
      } else { 
       jQuery('.search_home_row_2 > div').css('width', '100%'); 
      } 


     } else { 

      jQuery('.search_event_date').css('display', 'inline-block'); 
      jQuery(".listingTypeSelect input[value=studio]").attr('checked', false); // Unchecks it 
      jQuery(".listingTypeSelect input[value=teacher]").attr('checked', false); // Unchecks it 
      jQuery(".listingTypeSelect input[value=studio]").prop('disabled', true); 
      jQuery(".listingTypeSelect input[value=teacher]").prop('disabled', true); 

      if (jQuery(window).width() > 992) { 
       jQuery('.search_home_row_2 > div').css('width', '23.333%'); 
       jQuery('.search_home_row_2 .search_event_date').css('width', '30%'); 
      } else { 
       jQuery('.search_home_row_2 > div').css('width', '100%'); 
       jQuery('.search_home_row_2 .search_event_date').css('width', '100%'); 
      } 

     } 
    }); 

DOMに高いイベントをキャプチャする必要がある場合があります。 search_jobs divの例

jQuery(".search_jobs").on('change', '.listingTypeSelect input[value=event]', function() { 
    //Code 
}); 
+0

素晴らしい、これは魅力のように動作します。変更イベントを書くこの方法を知らなかった。ありがとう= D –

-1

フォームhttps://www.yogagids.nl/では、次のとおりです。

<form class="job_search_form" action="https://www.yogagids.nl/listings/" method="GET"> 

https://www.yogagids.nl/listings/上:

<form class="job_filters"> 

だから、それは別のHTMLをレンダリングについてです第2の場合には、アクションとメソッドの属性を持っていません。

関連する問題