开发者

Sum from field values and output the result inside div

开发者 https://www.devze.com 2023-03-10 03:13 出处:网络
I did this before with javascript, and have no idea to make it using jquery. I hope you can help me! I tried the next:

I did this before with javascript, and have no idea to make it using jquery.

I hope you can help me!

I tried the next:

<script>

$('.field').mouseout(function () {
    var sum = 0;
    $('.field').each(function() {
        sum += Number($(this).val());
    });
      $("#resultado").html(sum.toFixed(2));
});​​​​​​​​​

</script>

The div re开发者_如何学Gosultado should change the content. Not happening :/


Try the blur event, not the mouseout

$('.field').blur(function() {
    var sum = 0;
    $('.field').each(function() {
        sum += Number($(this).val());
    });
    $("#resultado").html(sum.toFixed(2));
});

http://jsfiddle.net/syJ9g/1/


or bind to all kinds of events

$('.field').bind("mouseout blur click", function() {
    var sum = 0;
    $('.field').each(function() {
        sum += Number($(this).val());
    });
    $("#resultado").html(sum.toFixed(2));
});

http://jsfiddle.net/syJ9g/2/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号