0

Using buttons on a numeric keypad to send back a value to the controller but the keypad wont let me go below 0 when I press the '-' button, the user may need to pass back a negative value.

Also if possible, if the user has -32 for example and they pres '-' twice it will change to +32. Can anyone give me a hand with this? not use to working with script? ta

Alot of 0's so unsure which one to change?? thanks

     <script>
        $(document).ready(function () {
            $('.numero').click(function () {
                if (!isNaN($('#myInput').val())) {
                    if (parseInt($('#myInput').val()) == 0) {
                        $('#myInput').val($(this).text());
                    } else {
                        $('#myInput').val($('#myInput').val() + $(this).text());
                    }
                }
            });
            $('.neg').click(function () {
                if (!isNaN($('#myInput').val()) && $('#myInput').val().length > 0) {
                    if (parseInt($('#myInput').val()) > 0) {
                        $('#myInput').val(parseInt($('#myInput').val()) - 1);
                    }
                }
            });
            $('.pos').click(function () {
                if (!isNaN($('#myInput').val()) && $('#myInput').val().length > 0) {
                    $('#myInput').val(parseInt($('#myInput').val()) + 1);
                }
            });
            $('.del').click(function () {
                $('#myInput').val($('#myInput').val().substring(0, $('#myInput').val().length - 1));
            });
            $('.clear').click(function () {
                $('#myInput').val('');
            });
            $('.zero').click(function () {
                if (!isNaN($('#myInput').val())) {
                    if (parseInt($('#myInput').val()) != 0) {
                        $('#myInput').val($('#myInput').val() + $(this).text());
                    }
                }
            });
        });
    </script>
John
  • 3,965
  • 21
  • 77
  • 163
  • You are checking for only positive number! Read your code and try to understand it, don't just copy/paste things... Really, start by putting some break point in your code to get a better visibility on how it works – A. Wolff Sep 18 '13 at 11:10
  • i did try and read and understand it, but there are references to both +1 and -1 and 0, but ok thanks – John Sep 18 '13 at 11:12
  • In .neg click handler, you have: `if (parseInt($('#myInput').val()) > 0)` so once its equal to 0, nothing happen. – A. Wolff Sep 18 '13 at 11:13
  • ah riteok thanks A man...still going to go through it here with a breakpoint though – John Sep 18 '13 at 11:21
  • what about if it was '-32' and i pressed '-' again, would it be possible to make this a '+32' – John Sep 18 '13 at 11:22
  • Sure it is possible! You should filter it inside keyup or keypress handler. – A. Wolff Sep 18 '13 at 11:25
  • so create a new button Switch – John Sep 18 '13 at 11:32
  • $('.switch').click(function () { if (!isNaN($('#myInput').val()) && $('#myInput').val().length > 0) { $('#myInput').val(parseInt($('#myInput').val()) - $('#myInput').val() x2); } }); – John Sep 18 '13 at 11:34
  • so would '-' to 0, then '-' again to get the negative value if it was originally a + – John Sep 18 '13 at 11:34

0 Answers0