当您单击此处第二个“客户”标题下的任何按钮时,页面将返回顶部:http: //kodiakgroup.com/clients.php
我已经尝试了该preventDefault
功能以及您在下面看到的更改功能中的return false
建议。看看我能做些什么来防止这种行为?
第一部分更改:
//Toggle select all/deselect function
$('#vertical-filters input').change(function (e) {
$('.selectAllBoxes').prop('checked', false);
getCustomers();
e.preventDefault();
return false;
});
$('.selectAllBoxes').change(function (e) {
$('#vertical-filters input').prop('checked', false);
getCustomers();
e.preventDefault();
return false;
});
所有的JavaScript:
$(function () {
$('.selectAllBoxes').prop('checked', true);//Set checkboxes as checked by default
getCustomers(); //Initially call all customers
function getCustomers()
{
$('ul#customers').html('');//empty list
var definedCategoryArray=new Array();
var categoriesPlural= new Array();
var customerSplit=new Array();
for(var x=0; x< $('#vertical-filters input').length; x++){
var thisItem=$('#vertical-filters input')[x];
var thisItemName=$(thisItem).attr('id');
if($('.selectAllBoxes').is(':checked')){
definedCategoryArray[thisItemName]=true;
}
else{
if ($(thisItem).is(':checked'))
definedCategoryArray[thisItemName]=true;
else
definedCategoryArray[thisItemName]=false;
}
}
$.getJSON('customers.json', function(data) {
for(var index in definedCategoryArray){ //cycle through categories array
for(var i=0; i<data.customers.length; i++){ //cycle through json data
if (definedCategoryArray[index]==true){//if the value in the array is true (item checked)
//console.log(data.customers[i].customerName+ ' : ' + data.customers[i].category);
if(data.customers[i].category.indexOf(',') != -1) //if there is more than one category, detect the comma seperating them
categoriesPlural = data.customers[i].category.split(',');
else //there is only one category
categoriesPlural[0]=data.customers[i].category;
for (var y = 0; y<categoriesPlural.length; y++){
//console.log(categoriesPlural[y]);
if(categoriesPlural[y] == index){ //match category (from definedCategoryArray index) to items in json object to parse
$('ul#customers').append('<li class="' +data.customers[i].customerName.replace(/\s+/g, '-') + '" id="'+data.customers[i].customerName.replace(/\s+/g, '-')+'"><a href="'+ data.customers[i].link +'" title="'+ data.customers[i].customerName +'" target="_blank"><img src="'+ data.customers[i].imageLink +'" alt="'+ data.customers[i].customerName +'" /></a></li>');
checkDuplicates(data.customers[i].customerName.replace(/\s+/g, '-'));
}
}
}
}
}
}).fail(function() { console.log( "error" ); });
}
function checkDuplicates(customerName){
for(var x=0; x< $('#customers li').length; x++){//loop through clients already on the page to prevent duplicates
var thisClient=$('#customers li')[x];
var thisClientName=$(thisClient).attr('id');
if(thisClientName == customerName){
var superClient1=$('.'+customerName)[1];
var superClient2=$('.'+customerName)[2];
if (superClient1)
$(superClient1).css('display','none');
if(superClient2)
$(superClient2).css('display','none');
//console.log(customerName + '=' + thisClientName + ' emptied');
}
}
}
//Toggle select all/deselect function
$('#vertical-filters input').change(function (e) {
$('.selectAllBoxes').prop('checked', false);
getCustomers();
e.preventDefault();
return false;
});
$('.selectAllBoxes').change(function (e) {
$('#vertical-filters input').prop('checked', false);
getCustomers();
e.preventDefault();
return false;
});
});