我希望此功能在用户点击时打印球图片的数量或消防车图片的数量:
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 次。
关于从这里去哪里有什么建议吗?
最佳答案
您只需将附加消息行移出 foreach 循环即可:
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.");
}
关于javascript - 尝试附加特定图像类型的图像计数 | jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40096017/