简体   繁体   中英

How to increment the two text box values when button is clicked?

 jQuery(document).ready(function() { // This button will increment the value $('.qtyplus').click(function(e) { e.preventDefault(); fieldName = $(this).attr('field'); //alert(fieldName); var currentVal = parseInt($('input[name=' + fieldName + ']').val()); if (!isNaN(currentVal)) { $('input[name=' + fieldName + ']').val(currentVal + 1000); } else { $('input[name=' + fieldName + ']').val(1000); } }); }); function buttonClick() { //alert("hi"); var n = document.getElementById('rs').value; alert(n); var i = 50; if (i == 50) { alert(i); var n = +n + +i; alert(n); } } 
 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <h3> Calculator</h3> <table style="width: 30%;"> <form name="calc" action="" method="post"> <tr> </tr> <tr> <td>quantity:</td> <td><input type="text" name="value1" /></td> </tr> <tr> <td>price:</td> <td> <input type='text' name='quantity' value='2000' class='qty' /> <input type='button' value='+' class='qtyplus' field='quantity' onclick="buttonClick()" /> </td> </tr> <tr> <td>discount:</td> <td><input type='text' name='rs' value='500' field='rs' id='rs' class='counter' onclick="buttonClick()" /></td> </tr> <tr> </tr> </form> </table> 

In above HTML code, when button is clicked, I have to increment the two text box values

For example, click a button, price text box value will be 3000, discount text box value will be 550 price text box value is incremented, but discount value will not be changed. Second script are run,but the text box value are not changed.

You just forget to change the value of the discount input.

<script type="text/javascript">//second script
    function buttonClick() {
        //alert("hi");
        var n = document.getElementById('rs').value;
        alert(n);
        var i = 50;
        if (i == 50) {
            alert(i);
            var n = +n + +i;
            alert(n);
            $("#rs").val(n) // add this line
        }
    }
</script>

You have not written anything to change value of discount textbox.

you can do as follows:

$('.qtyplus').click(function (e) {
     e.preventDefault();
            qty = $('.qty').val();
            discount = $('#rs').val();

            var currentQtyVal = parseInt(qty);
            if (!isNaN(currentQtyVal)) {
                $('input[name=quantity]').val(currentQtyVal + 1000);
            } else {
                $('input[name=quantity]').val(1000);
            }

            var currentDiscountVal = parseInt(discount);
             if (!isNaN(currentDiscountVal)) {
                $('input[name=rs]').val(currentDiscountVal + 50);
            } else {
                $('input[name=rs]').val(50);
            }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM