限制多个输入字段的总和值

时间:2013-05-13 09:58:51

标签: javascript jquery

我需要一些javascript或jQuery的帮助。

我有三(3)个我想要过滤的输入字段(三个字段的总和)。

3个输入字段的总值必须为100.如果用户填写的数量超过100,则会自动更改总数为100的值。

您可以看到this example

<input type="text" name="text1" size="3"> text1<br />
<input type="text" name="text2" size="3"> text2<br />
<input type="text" name="text3" size="3"> text3<br />

    <p>The maximum value of total 3 fields above is 100.</p>
    <pre>example 1 :

    text1 : 20;
    text2 : 30;
    text3 : 50; (will automatically filled with 50 because the total value must 100)</pre>
    <pre>example 2 :

    text1 : 37;
    text2 : 60;
    text3 :  3; (will automatically filled with 3 because the total value must 100)</pre>

谢谢你的帮助, 我需要它:))

4 个答案:

答案 0 :(得分:2)

$("input:lt(2)").on("change", function() {
    var other = $("input:lt(2)").not(this).val();
    if (other.length)
        $("input:eq(2)").val(100 - this.value - other);
});

DEMO: http://jsfiddle.net/D5F3p/3/

答案 1 :(得分:0)

这是我能想到的最简单的事情!

$(document).ready(function(){
    $('input[name="text2"]').blur(function(){
        $('input[name="text3"]').val(100 - $('input[name="text1"]').val() - $('input[name="text2"]').val());
    });
});

IMO,你可以这样做:

  1. 两个输入只能输入两个字符。不要超过这个!
  2. 您还需要检查两个输入的总和是否不应超过101!
  3. 保持输入2只读,直到在输入1中输入了某些内容。
  4. 保持输入3始终只读。
  5. 小提琴:http://jsfiddle.net/praveenscience/D5F3p/5/

答案 2 :(得分:0)

<head>
<script type="text/javascript">
function check() {
    var sum = 0;
    var inputs = $(".test");
    for (i = 0; i < inputs.length; i++) {
        if (inputs[i].value == parseInt(inputs[i].value)) {
            sum += parseInt(inputs[i].value);
            if (sum > 100) {
                sum -= parseInt(inputs[i].value);
                inputs[i].value =  100-sum;

             }
         }
    }

 }



</script>
</head>
<body>
<input type="text" onkeyup="check()" class="test" size="3"> 
<input type="text" onkeyup="check()" class="test" size="3">
<input type="text" onkeyup="check()" class="test" size="3"> 

答案 3 :(得分:0)

   $(function(){

$('input[name="text1"]').keyup(function(){

    var text1Value = $('input[name="text1"]').val();
    if(text1Value >=100)
    {
     $('input[name="text1"]').val(100);
     $('input[name="text2"]').val('');
     $('input[name="text3"]').val('');
     $('input[name="text2"]').attr('disabled','disabled'); 
     $('input[name="text3"]').attr('disabled','disabled'); 
    }
    else
    {
     $('input[name="text2"]').removeAttr('disabled'); 
     $('input[name="text3"]').removeAttr('disabled');
    }
});
$('input[name="text2"]').keyup(function(){
    var text1Value = parseInt($('input[name="text1"]').val());
    var text2Value = parseInt($('input[name="text2"]').val());
    if(isNaN(text1Value))
    {
     text1Value =0;
    }

    if(isNaN(text2Value))
    {
     text2Value =0;
    }
    var total = text1Value + text2Value;

    if(total >=100)
    {
     $('input[name="text2"]').val(100-text1Value);
     $('input[name="text3"]').val('');
     $('input[name="text3"]').attr('disabled','disabled'); 
    }
    else
    {
     $('input[name="text3"]').removeAttr('disabled');
     $('input[name="text3"]').val(100-total);
    }
});

});

我正在检查所有文本框的总和是否超过100,然后在最后输入的文本框中进行更正并禁用下一个文本框。

相关问题