我有一个循环,经过一个特定的类(.casebox)。它检查其他类,并通过.load()更改标题并加载特定内容。
HTML
<div id="mainbody">
<div class="halfbox">
<h4 class="box casetitle"></h4>
<div class="caseloader loader"><img src="<?php echo $url_resources_images; ?>ajax-loader.gif" alt="Loading..." /><br /><p>Please be patient, loading cases...</p></div>
<div class="casebox dashbox"></div>
</div>
<div class="halfbox">
<h4 class="box casetitle"></h4>
<div class="caseloader loader"><img src="<?php echo $url_resources_images; ?>ajax-loader.gif" alt="Loading..." /><br /><p>Please be patient, loading cases...</p></div>
<div class="casebox dashbox apending"></div>
</div>
的Javascript
function loadCases(){
$(".casebox").each(function(){
getURLs();
boxType();
console.log('activated');
div = $(this).parents('div').eq(0);
div.css('background', 'blue');
//BOXOVERIDE - FOR WHEN THE URL CAN'T BE SET E.G. DASHBOARD
if($(this).hasClass("allopen")){
div.find($('.casetitle').text('All Open Cases'));
caseGET = "&casetype=allopen";
} else if($(this).hasClass("copen")){
div.find($('.casetitle').text('My Created Cases (Open)'));
caseGET = "&casetype=copen";
} else if($(this).hasClass("aopen")){
div.find($('.casetitle').text('My Assigned Cases (Open)'));
caseGET = "&casetype=aopen";
} else if($(this).hasClass("allclosed")){
div.find($('.casetitle').text('All Closed Cases'));
caseGET = "&casetype=allclosed";
} else if($(this).hasClass("cclosed")){
div.find($('.casetitle').text('My Created Cases (Closed)'));
caseGET = "&casetype=cclosed";
} else if($(this).hasClass("aclosed")){
div.find($('.casetitle').text('My Assigned Cases (Closed)'));
caseGET = "&casetype=aclosed";
} else if($(this).hasClass("apending")){
div.find($('.casetitle').text('My Assigned Cases (Pending Close)'));
caseGET = "&casetype=apending";
}
console.log(caseGET);
$(this).load("boxes/cases.php?boxtype=" + boxtype + caseGET + orgGET, function (){
div.find(".caseloader").hide();
});
});
}
然而,它似乎快速运行,当它进入第二个循环时,它似乎覆盖了第一次运行时所做的设置。第一次运行没有.hasClass的匹配结果,其标题由函数getURLS()设置。然而,当第二个循环通过时,它会覆盖第一个框中设置的内容,并将标题设置为“My Assigned Cases(Pending Close)”,即使它只是具有此类的第二个框。除此之外,第一个框加载正确的内容,但加载栏不会消失,但第二个框上的加载栏确实消失。
关于如何确保它的任何想法?这可能不是很清楚,但希望从上面的代码中,它将有助于解释。感谢。
答案 0 :(得分:-1)
我不确定boxType,orgGet来自哪里......但我认为问题是当你声明像$('。casetitle')这样的东西时,它实际上会重新搜索整个文档中的那些类。
尝试类似:
function loadCases(){
$(".casebox").each(function(){
getURLs();
boxType();
console.log('activated');
//parent halfbox - only get the first parent
var parent = $(this).parent('.halfbox');
parent.css('background', 'blue');
//seems that you try to get the casetitle all the time
//find the element with the class casetitle, which is inside the parent
var ct = parent.find('.casetitle');
//BOXOVERIDE - FOR WHEN THE URL CAN'T BE SET E.G. DASHBOARD
if($(this).hasClass("allopen")){
//change the html inside the h4
ct.html('All Open Cases');
caseGET = "&casetype=allopen";
} else if($(this).hasClass("copen")){
ct.html('My Created Cases (Open)');
caseGET = "&casetype=copen";
} else if($(this).hasClass("aopen")){
ct.html('My Assigned Cases (Open)');
caseGET = "&casetype=aopen";
} else if($(this).hasClass("allclosed")){
ct.html('All Closed Cases');
caseGET = "&casetype=allclosed";
} else if($(this).hasClass("cclosed")){
ct.html('My Created Cases (Closed)');
caseGET = "&casetype=cclosed";
} else if($(this).hasClass("aclosed")){
ct.html('My Assigned Cases (Closed)');
caseGET = "&casetype=aclosed";
} else if($(this).hasClass("apending")){
ct.html('My Assigned Cases (Pending Close)');
caseGET = "&casetype=apending";
}
console.log(caseGET);
$(this).load("boxes/cases.php?boxtype=" + boxtype + caseGET + orgGET, function (){
parent.find('.caseloader').hide();
});
});
};