2012-02-19 44 views
0

我正在尝试这段代码并且添加一块。我希望#total拥有最大数量。我甚至不知道如何解决这个问题。我在添加验证或某事物时查看了查询文档,但这不是我想要的。任何帮助将不胜感激。 http://jsfiddle.net/anderskitson/VVkzG/1/jquery input field added,最大总数

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); 
    } 
};​ 
+0

喜欢这个? http://jsfiddle.net/VVkzG/2/ – 2012-02-19 06:43:16

+0

准确地说,您可以将此作为答案,以便我可以将其标记为这样。 – 2012-02-19 22:06:51

回答

2

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); 
     } 


    } 
};​ 
+0

请将您的代码发布在您的答案中,有一天jsfiddle可能不在那里。 – Madbreaks 2012-02-24 05:38:11