Subtracting numeric contents of one text field from another and displaying the results in real time? [closed]
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this questionI am using jQuery keypad to build a point of sale interface
http://keith-wood.name/keypadBasics.html
I have two tex开发者_Go百科t fields namely
PREVIOUS RENT and RENT PAID
as the user enters data I would like the page to run the following formula in real-time
PREVIOUS RENT - RENT PAID
I would like it to display the results below the text fields in real-time (without refreshing the page or submitting the code)
How would this be done?
Binding to change or blur should work.
<html>
<body>
  <script src='jquery.js'></script>
  <script>
    $(document).ready(function(){
      $('#previousRent').change(function(){
        calcResult();
      });
      $('#rentPaid').change(function(){
        calcResult();
      });
    });
    function calcResult() {
      $('#result').val( parseFloat($('#previousRent').val() - $('#rentPaid').val()) );
    }
  </script>
  <input type="text" id="previousRent">
  <input type="text" id="rentPaid">
  <input type="text" id="result">
</body>
</html>
Assuming you want to update to happen when the 'rent paid' field loses focus:
$('#rent-paid').blur(function({
      var diff =  $('#previous-rent').attr('value') - $(this).attr('value');
      $('#total').text(diff);
 }));
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论