开发者

Subtracting numeric contents of one text field from another and displaying the results in real time? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.

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 question

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜