jquery输入字段相加,最大总计

发布于 2025-01-06 23:33:47 字数 658 浏览 4 评论 0原文

我正在尝试获取这段代码并向其中添加一段代码。我希望 #total 有一个最大数量。我什至不知道如何处理这个问题。我查看了查询文档以添加验证或其他内容,但这不是我想要的。任何帮助将不胜感激。 http://jsfiddle.net/anderskitson/VVkzG/1/

JQUERY:

$('#the_input_id').keyup(function() {
    updateTotal();
});

$('#the_input_id1').keyup(function() {
    updateTotal();
});

var updateTotal = function() {
    var input1 = parseInt($('#the_input_id').val());
    var input2 = parseInt($('#the_input_id1').val());
    if (isNaN(input1) || isNaN(input2)) {
        $('#total').text('');
    } else {
        $('#total').text(input1 + input2);
    }
};​

I am trying to take this code and add one piece to it. I would like the #total to have a maximum number. I am not even sure how to approach this one. I have looked around the query documentation at adding a validation or something but it wasn't what I wanted. Any help would be greatly appreciated. http://jsfiddle.net/anderskitson/VVkzG/1/

JQUERY:

$('#the_input_id').keyup(function() {
    updateTotal();
});

$('#the_input_id1').keyup(function() {
    updateTotal();
});

var updateTotal = function() {
    var input1 = parseInt($('#the_input_id').val());
    var input2 = parseInt($('#the_input_id1').val());
    if (isNaN(input1) || isNaN(input2)) {
        $('#total').text('');
    } else {
        $('#total').text(input1 + input2);
    }
};​

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

想你只要分分秒秒 2025-01-13 23:33:47

jsfiddle.net/VVkzG/2

HTML

<form method="post">
    <input type="text" id="the_input_id">
    <input type="text" id="the_input_id1">
</form>

</div>
<div id="total">

</div>​

JavaScript

$('#the_input_id').keyup(function() {
    updateTotal();
});

$('#the_input_id1').keyup(function() {
    updateTotal();
});

var updateTotal = function() {
    var input1 = parseInt($('#the_input_id').val());
    var input2 = parseInt($('#the_input_id1').val());
    if (isNaN(input1) || isNaN(input2)) {
        $('#total').text('');
    } else {
        var max = 500;
        var total = input1 + input2;

        if(total > max) {
            $('#total').text('The maximum is '+max);
        } else {
             $('#total').text(total);
        }


    }
};​

jsfiddle.net/VVkzG/2

HTML

<form method="post">
    <input type="text" id="the_input_id">
    <input type="text" id="the_input_id1">
</form>

</div>
<div id="total">

</div>​

Javascript

$('#the_input_id').keyup(function() {
    updateTotal();
});

$('#the_input_id1').keyup(function() {
    updateTotal();
});

var updateTotal = function() {
    var input1 = parseInt($('#the_input_id').val());
    var input2 = parseInt($('#the_input_id1').val());
    if (isNaN(input1) || isNaN(input2)) {
        $('#total').text('');
    } else {
        var max = 500;
        var total = input1 + input2;

        if(total > max) {
            $('#total').text('The maximum is '+max);
        } else {
             $('#total').text(total);
        }


    }
};​
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文