请参考以下代码:
var index=0;
var load=0;
$("td.load_ads").each(function(){
var loading=$(this);
$.post('/self_coded_helpers/jpost_get_ads.php',{index:index,type:'fetch_id'},function(data){
if($("input.exist").length > 0){
$("input.exist").each(function(){
if($(this).val()==data){
load='0';
}else{
load='1';
}
});
}else{
load='1';
}
if(load == 1){
$.post('/self_coded_helpers/jpost_get_ads.php',{index:index,type:'fetch_details',id:data},function(data2){
$("div.main_ads_div").append('<input type="hidden" class="exist" name="exist" value="'+data+'">');
if(data2!=0){
loading.html(load+'--'+data2);
}else{
loading.html("Place a Free Ad Now!");
}
});
}else{
loading.html(load+"--"+data+"Place a Free Ad Now!");
}
});
index=index+1;
});
我所做的是input
每次循环时都附加一个元素td.load_ads
:
$("div.main_ads_div").append('<input type="hidden" class="exist" name="exist" value="'+data+'">');
希望我能在下一次相同的循环中得到它.each
。
但是 var load 一直返回 1,因为在这种特殊情况下,我的 input.exist 长度总是错误的。
是因为输入没有那么快附加吗?
HTML 代码:
<table id="main_ads_table" col="7" row="4">
<tr>
<td colspan="8"></td>
</tr>
<tr>
<td class="load_img"></td>
<td class="load_img"></td>
<td class="load_img"></td>
<td class="load_img"></td>
<td class="load_img"></td>
<td class="load_img"></td>
<td class="load_img"></td>
<td class="load_img"></td>
</tr>
<tr>
<td class="load_ads"></td>
<td class="load_ads"></td>
<td class="load_ads"></td>
<td class="load_ads priority"></td>
<td class="load_ads priority"></td>
<td class="load_ads"></td>
<td class="load_ads"></td>
<td class="load_ads"></td>
</tr>
<tr>
<td class="load_ads"></td>
<td class="load_ads"></td>
<td class="load_ads"></td>
<td class="load_ads priority"></td>
<td class="load_ads priority"></td>
<td class="load_ads"></td>
<td class="load_ads"></td>
<td class="load_ads"></td>
</tr>
<tr>
<td class="load_ads"></td>
<td class="load_ads priority"></td>
<td class="load_ads priority"></td>
<td colspan="2">
<div id="main">
<?php if ($this->countModules('position-12')): ?>
<div id="top"><jdoc:include type="modules" name="position-12" />
</div>
<?php endif; ?>
<jdoc:include type="message" />
<jdoc:include type="component" />
</div><!-- end main -->
</td>
<td class="load_ads priority"></td>
<td class="load_ads priority"></td>
<td class="load_ads"></td>
</tr>
<tr>
<td class="load_ads"></td>
<td class="load_ads"></td>
<td class="load_ads"></td>
<td class="load_ads priority"></td>
<td class="load_ads priority"></td>
<td class="load_ads"></td>
<td class="load_ads"></td>
<td class="load_ads"></td>
</tr>
<tr>
<td class="load_ads"></td>
<td class="load_ads"></td>
<td class="load_ads"></td>
<td class="load_ads priority"></td>
<td class="load_ads priority"></td>
<td class="load_ads"></td>
<td class="load_ads"></td>
<td class="load_ads"></td>
</tr>
</table>
.POST 脚本
$type=$_POST['type'];
function limit_words($string, $word_limit)
{
$words = explode(" ",$string);
return implode(" ",array_splice($words,0,$word_limit));
}
if($type=='fetch_id'){
$query="SELECT * FROM ads1_adsmanager_ads ORDER BY RAND() LIMIT 1";
$db->setQuery($query);
$ad_details=$db->loadRow();
echo $ad_details[0];
}elseif($type=='fetch_details'){
$id=$_POST['id'];
$query="SELECT * FROM ads1_adsmanager_ads WHERE id=$id";
$db->setQuery($query);
$ad_details=$db->loadRow();
if(!empty($ad_details)){
echo '<input type="hidden" name="id" value="'.$ad_details[0].'">';
echo limit_words($ad_details[9],5);
echo '<br />';
echo limit_words($ad_details[10],20);
}else{
echo '0';
}
}
添加了我的 HTML 和 Post Script。看看它是否可以提供更多帮助。