尝试添加特定图像类型的图像计数| jQuery
Attempting to append an image count for specific image types | jQuery
我想让这个函数在用户点击时打印小球图片的数量或消防车图片的数量:
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.");
}
我注意到一些事情。第一件事是$(this).prop("checked")
将在<img>
标记上被调用,除非您将它们设置为在此处未显示的代码中的其他地方进行检查,否则不会检查。(即truckCount
和ballCount
不加1)
第二是$('#radioButtons').append(...)
在你的每个"循环"中被调用,所以它将为每个与选择器匹配的<img>
标签附加那行。
相关文章:
- 使用带括号的图像URL作为jQuery的背景
- 如何使用jquery将base64图像路径转换为真实路径
- 重新加载/替换图像 jQuery 作为
- 中的列表项
- 鼠标悬停在图像 jquery 上
- 使用图像 jQuery 切换
- 检查显示的(随机)图像是否与上一个(随机)图像(jQuery和JavaScript)相同
- 更改背景图像 jQuery
- 单击函数不在图像jQuery上启动
- 预览图像Jquery脚本don't运行良好
- 使用图像Jquery对话框预加载iFrame
- jQuery脉动代码与滚动图像jQuery代码冲突
- 检测粘贴事件在浏览器中的图像(jquery/javascript)
- 图像库与覆盖和html内部不能覆盖正确的图像.jquery
- 预加载图像- JQuery
- 使用HTML扩展图像&Jquery
- 从Rest API获取图像.JQuery的问题
- 使用尽可能多的独立线程检索5个图像- jquery ajax
- 点击图像jQuery -函数不工作后追加
- 如何使用XML数据更改网站上的图像&jQuery
- 在不向服务器发出新请求的情况下更改图像jQuery