我有图像,必须为每张图片计算facebook份额。我无法为此做出循环。现在只有一个元素。怎么做? 我的代码是:
<script>
$(document).ready(function(){
var current = 0;
$count = $('#count_bombs').val();
$('#' + current).each(function() {
var id = $('#' + current).val(); console.log(id);
$.getJSON('https://graph.facebook.com/http://example.com/images/view/' + id, function(data) {
document.getElementById("shares_count_" +current).innerHTML = data.shares;
console.log(data.shares); console.log("#shares_count_" +current);
});
current++;
});
});
</script>
<?php $num=0; $id = 0; ?>
<?php foreach($images as $key=>$val){ ?>
<div>
<a href="https://www.facebook.com/sharer/sharer.php?u=<?php echo URL::base();?>images/view/<?php echo $val['image_name'];?>">
<span aria-hidden="true" class="glyphicon glyphicon-link"></span>
<input type="hidden" id="<?php echo $id; ?>" class="count" value="<?php echo $val['image_name'];?>">
SHARE
</a>
</div>
<div>
<span class="glyphicon glyphicon-comment" aria-hidden="true" ></span>
<span id="shares_count_<?php echo $id; ?>"></span>
<input type="hidden" value="<?php echo count($bombs);?>" id="count_bombs" name="count_bombs">
</div>
编辑:我做到了。我使用了每种方法的index
参数。这是我的代码:
<script>
$(document).ready(function(){
var current = 0;
$("input[type=hidden]").each(function(index, element){
var id = $('#' + current).val();
$.getJSON('https://graph.facebook.com/http://pbombd.dev/bombs/view/' + id, function(data) {
$('#shares_count_' + index).html(data.shares);
});
current++;
});
});
</script>
答案 0 :(得分:1)
count
将只返回一个元素,因此无法循环。您似乎希望在页面中找到css类$('input.count').each(function() {
$input = $(this);
// $input contains the hidden input
console.log($input.attr('id));
});
的每个隐藏输入。你可以这样做:
{{1}}