doujianwei8217 2013-07-17 07: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 07: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条)
编辑
预览

报告相同问题?