计数+/-对于复选框,单击并取消单击 [英] Count + / - for checkbox click and unclick

查看:107
本文介绍了计数+/-对于复选框,单击并取消单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户单击复选框时,计数器#counter +1就可以了,但是当他取消选中该复选框时,对于同一复选框则不会为-1.当他们单击特定的复选框并取消选中-1时,我想+1.

When a user clicks the checkbox the counter #counter +1, thats fine, but when he unchecks it does not -1 for the same checkbox. I want to +1 when they click a particular checkbox and when unchecking -1.

-JS-

$('.CheckBox').toggle(function(){
    $('#Counter').html('( '+i+' Selected )');
    i++;
}, function() {

    $('#Counter').html('( '+i+' Selected )');
    i--;
});

-PHP ---

do { ?>

<div style="position:relative; width:100%; height:20px; background-color:#FF5300;" class="CheckBox" id="<?php echo $row_EX['x1']; ?>">

<input type="checkbox" name="<?php echo $row_EX['x2']; ?>" value="<?php echo $row_EX['x3']; ?>" style="cursor:pointer; ">

<span style="position:relative; top:-2px; font-family:arial; color:#000; font-size:12px;"><?php echo $row_EX['lx4']; ?></span>

</div>

<div style="height:1px; width:1px;"></div>
<?php } while ($row_EX = mysql_fetch_assoc($EY)); ?>    

<span style="position:relative; left:10px; top:6px; font-family:arial; font-size:16px;" id="counter">(0 Selected)</span>

推荐答案

$('.CheckBox').change(function() {
  if (this.checked) {
    i++;
  } else {
    i--;
  }
  $('#Counter').html('( '+i+' Selected )');
});

确保在页面加载时初始化var i = 0;.

Make sure you initialise var i = 0; when the page loads.

这篇关于计数+/-对于复选框,单击并取消单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆