jQuery复选框SUM值

时间:2013-08-28 12:26:58

标签: javascript jquery html

我的代码有问题。它不会复原所有复选框的值。这有什么问题?

我已经尝试了所有方法,但它不会向我展示任何东西。

HTML

<div id='vizitkarte'>
    <h2>Vizītkarte</h2>

    <input id='cheackbox_pam_viz' type="checkbox" value="189.95" name="choise" /> <a id='cena_viz' title="189,95 Ls" value="189.95" name="choise" class="cena"></a> 
    <input class="checkbox" type="checkbox" value="" name="choise" /> <a class='popup' rel="popup1"></a><a title="9,95 Ls" value="9.95" name="choise" class="cena"></a>

</div>
<div> <span id="span"></span>
User total usage: <span id="usertotal"> </span>

的JavaScript

function displayVals() {
      calcUsage();
      var singleValues = $("#more").val();         
      $("#span").html("<b>more addons:</b> " + 
                  singleValues + ' EUR');
}
var $cbs = $('input[name="choise"]');
function calcUsage() {
    var total = $("#more").val();
    $cbs.each(function() {
        if (this.checked)
            total = parseInt(total) + parseInt(this.value);
    });
    $("#usertotal").text(total + ' EUR');
}

    $("select").change(displayVals);
    displayVals();
//For  checkboxes

$cbs.click(calcUsage);

jsFiddle

2 个答案:

答案 0 :(得分:1)

如果我明白你要做什么,那么这个“有效”。但是......我不知道应该使用$("#more").val()选择什么,因为没有任何带有该ID的HTML元素。所以...我做了那个。

HTML

<div id='vizitkarte'><h2>Vizītkarte</h2>
    <input id='cheackbox_pam_viz' class="checkbox" type="checkbox" value="189.95" name="choise" />
    <a  id='cena_viz' title="189,95 Ls" value="189.95" name="choise" class="cena"></a>  
    <input class="checkbox" type="checkbox" value="189.95" name="choise" /> 
    <a class='popup' rel="popup1"></a><a title="9,95 Ls" value="9.95" name="choise" class="cena"></a>
</div>
<div> <span id="span"></span>
User total usage: <span id="usertotal"> </span>

JQuery的

function displayVals() {
      calcUsage();
      var singleValues = 0; //$("#more").val();         
      $("#span").html("<b>more addons:</b> " + 
                  singleValues + ' EUR<br>');
}
var $cbs = $('.checkbox');
function calcUsage() {
    var total = 0; //$("#more").val();
    $cbs.each(function() {
        if ($(this).is(":checked"))
            total = parseInt(total) + parseInt($(this).val());
    });
    $("#usertotal").text(total + ' EUR');
}

    $("select").change(displayVals);
    displayVals();
//For  checkboxes

$cbs.click(function() {
    calcUsage();
});

JSFiddle:http://jsfiddle.net/G2nTB/18/

答案 1 :(得分:0)

试试这个:http://jsfiddle.net/G2nTB/6/

HTML

<div id='vizitkarte'>
 <h2>Vizītkarte</h2>
<input type="checkbox" value="189.95" name="choise">189,95</input>
<input type="checkbox" value="9.95" name="choise">9,95</input>
</div>

<div>User total usage: <span id="usertotal"> </span>

的Javascript

function displayVals()
{
  calcUsage();
  var singleValues = $("#more").val();         
  $("#span").html("<b>more addons:</b> " +  singleValues + ' EUR');
}
var $cbs = $('input[name="choise"]');
function calcUsage()
{
var total = 0;

$cbs.each(function() {
    if (this.checked)
    {
        total += parseInt(this.value);

    }

});
$("#usertotal").text(total + ' EUR');
}

$("select").change(displayVals);
displayVals();

//For  checkboxes 
$cbs.click(calcUsage);