尝试为特定图像类型附加图像计数| jQuery的

时间:2016-10-17 21:32:48

标签: javascript jquery html

我希望此功能能够在用户点击时打印球图片的数量或救火车图片的数量:

function imageCounter (){

var ballCount = 0;
var truckCount = 0;

$('img[src*=ball]').each(function(){
    if($(this).prop("checked")){
        ballCount+=1;
    }     
    $('#radioButtons').append("There are " + ballCount + " ball images.");
 });


$('img[src*=truck]').each(function(){
    if($(this).prop("checked")){
        truckCount+=1;
    }
    $('#radioButtons').append("There are " + truckCount + " truck images.");
    });

}

我觉得我很亲密..

我有4个球图像和1个救火车图像。我的代码目前没有给我图像的数字值,而是打印输出消息4次为球,1次为救火车。

关于从哪里开始的任何建议?

2 个答案:

答案 0 :(得分:1)

你只需要将追加消息行移出foreach循环:

and card_num in (select card_num
                 from e_transaction 
                 ...)

答案 1 :(得分:1)

我注意到了一些事情。首先要在$(this).prop("checked")标记上调用<img>,除非您将其设置为在此处未显示的代码中的其他位置进行检查,否则不会对其进行检查。 (意思是truckCountballCount不会增加)

其次是在每个“循环”中调用$('#radioButtons').append(...)的事实,所以它将为每个与选择器匹配的<img>标记附加该行。