gpt4 book ai didi

javascript - 附加 .each 并在下一个循环 jquery 中获取值

转载 作者:行者123 更新时间:2023-11-30 05:52:01 25 4
gpt4 key购买 nike

请引用以下代码:

    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;
});

我所做的是在每次遍历 td.load_ads 时附加一个 input 元素:

$("div.main_ads_div").append('<input type="hidden" class="exist" name="exist" value="'+data+'">');

希望我能在下一次循环相同的 .each 时得到它。

但 var load 一直返回 1,因为在这种特殊情况下我的 input.exist 长度始终为 false。

是因为输入没有那么快地附加吗?

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。看看它是否能提供更多帮助。

最佳答案

你让自己很难过。您不需要先收集 ID 或使用 ajax 获取每个单独的广告。通过将所有内容都放在 1 个帖子中,您首先就不会遇到重复问题。

jpost_get_ads.php:

function limit_words($string, $word_limit)
{
$words = explode(" ",$string);
return implode(" ",array_splice($words,0,$word_limit));
}

$count = (int)$_POST['count'];
$query="SELECT id, title, description FROM ads1_adsmanager_ads ORDER BY RAND() LIMIT $count";
$db->setQuery($query);
$ad_details = array();

while ($row = $db->loadAssoc()) {
$key = array_search($ids, $row['id']);
$row['title'] = limit_words($row['title'],5);
$row['description'] = limit_words($row['description'],20);

$ad_details[$key] = $row;
}

header('Content-Type: application/json');
echo json_encode($ad_details);
exit();

JavaScript

var count = $("td.load_ads").length;

$.post('/self_coded_helpers/jpost_get_ads.php', { count: count } function(ads){
$("td.load_ads").each(function(){
var ad = ads.unshift()
, content;

if (ad) {
content = ad.title + "<br>" + ad.description;
} else {
content = "Place a Free Ad Now!";
}

$(this).html(content);
});
});

关于javascript - 附加 .each 并在下一个循环 jquery 中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14149448/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com