我有一个Wordpress页面,当有人点击复选框时,我正在运行一些jQuery来显示和隐藏2个输入字段。问题是这个代码在我的主页上完美运行但是当我尝试在子页面上做同样的事情(使用完全相同的html结构和jquery)时,它不再有效。这并不意味着文件没有被加载,因为jQuery文件的1个部分在该页面上运行良好,我甚至在文件末尾有一个console.log来显示文件已满载。
www.yogagids.nl是主页,当您点击复选框" Evenementen"显示2个datepicker字段,取消选择它们并隐藏它们。那应该怎么做。
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 :(得分:0)
我几乎可以肯定,当您绑定change事件时,复选框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
});
答案 1 :(得分:-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而在第二种情况下你没有动作和方法属性。