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 socket通信实现多人聊天室疑惑
  • ¥15 DEV-C++编译缺失
  • ¥33 找熟练码农写段Pyhthon程序
  • ¥100 怎么让数据库字段自动更新
  • ¥15 antv g6 力导向图布局
  • ¥15 quartz框架,No record found for selection of Trigger with key
  • ¥15 锅炉建模+优化算法,遗传算法优化锅炉燃烧模型,ls-svm会搞,后面的智能算法不会
  • ¥20 MATLAB多目标优化问题求解
  • ¥15 windows2003服务器按你VPN教程设置后,本地win10如何连接?
  • ¥15 求一阶微分方程的幂级数