doujianwei8217 2013-07-17 15:37
浏览 89
已采纳

然后,如果选中,则将复选框值添加到变量中

I found this example:

The Idea is to get the checked checkbox value, and use it further as avariable:

Maybe this example is not the best, but I only found this one as it is much closer to what I want.

See FIDDLE

<div id="pakker">
<input type="checkbox" checked="checked" value="39" />test 1<br/>
<input type="checkbox" value="79" />test 2<br/>
<input type="checkbox" value="29" />test 3<br/>
<input type="checkbox" value="49" />test 4<br/>

<script type="text/javascript">
jQuery(document).ready(function($) {
    var sum = 0;
    $('#pakker :checkbox').click(function() {
        sum = 0;
        $('#pakker :checkbox:checked').each(function(idx, elm) {
            sum += parseInt(elm.value, 10);
        });

        $('#sum').html(sum);

    });
});
</script>
  1. As you can see it shows values only after mouse click checkbox. How can I make it to get the checked checkbox value on Loading page? Example: http://jsfiddle.net/vaKWs/31/

  2. if more then one checkbox is selected it ADDS the numbers. is it possible to show the valuse like (93, 25, 256 etc) without adding them?

  3. Is it possible the result to put into a VARIABLE so that i can use this variable in other functions on the page? For example if $variable { then } esle {...

* PS * I am new with php and js. Thank you for understanding!

  • 写回答

3条回答 默认 最新

  • douren7921 2013-07-17 15:43
    关注
    jQuery(document).ready(function($) {
    var sum = 0;
    $('#pakker :checkbox').click(function() {
        sum = 0;
        $('#pakker :checkbox:checked').each(function(idx, elm) {
            sum += parseInt(elm.value, 10);
        });
    
        $('#sum').html(sum);
    
    });
    $('#pakker :checkbox[checked]').each(function(idx, elm){
    sum += parseInt(elm.value, 10);
    });
    $('#sum').html(sum);
    });
    

    in HTML:

        <input type="checkbox" checked value="39" />test 1<br/>
    

    http://jsfiddle.net/vaKWs/6/

    There you go.

    Please rate my answer.

    that would really help me!!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致