简体   繁体   中英

Multiply and add 2 input boxes using jquery

I would like the script to multiply values from input boxes by the values of hidden inputs, and print the result upon clicking the button.

Script pretty much does it but only for 1 input.

Any easy way to do it?

Much appreciated.

Fiddle: http://jsfiddle.net/eN7S6/3/

 <table>
        <tr>
            <td>
    <input name="inputone" id="inputone" class="calc" value="1"><span id="Total"></span>
                </td>
        </tr>
            <tr>
            <td>
    <input name="inputone" id="inputone" class="calc" value="1"><span id="Total"></span>
                </td>
                </tr>
    </table>
        <input name="inputtwo" id="inputtwo" class="calc" value="2" type="hidden" readonly>
        <input name="inputtwo" id="inputtwo" class="calc" value="3" type="hidden" readonly>
        <input type="button" id="update" value="Calculate" />

And jQuery

$(document).ready(function() {      
    $('#update').click(function() { 
        var inputone = $('#inputone').val();
        var inputtwo = $('#inputtwo').val();
        var totalTotal = ((inputone * 1) * (inputtwo * 1));     
        $('#Total').text(totalTotal);
    });
}); 

Your element IDs are not unique. You have duplicates, such as "inputone" and "inputtwo".

I've changed their names, and added a new total span.

Here's the updated code:

<table>
    <tr>
        <td>
<input name="inputone" id="inputone" class="calc" value="1"><span id="TotalOne"></span>
            </td>
    </tr>
        <tr>
        <td>
<input name="inputtwo" id="inputtwo" class="calc" value="1"><span id="TotalTwo"></span>
            </td>
            </tr>
</table>
    <input name="multiplierone" id="multiplierone" class="calc" value="2" type="hidden" readonly>
    <input name="multipliertwo" id="multipliertwo" class="calc" value="3" type="hidden" readonly>
    <input type="button" id="update" value="Calculate" />

and jQuery:

$(document).ready(function() {      
    $('#update').click(function() { 
        var inputone = $('#inputone').val();
        var multiplierone = $('#multiplierone').val();
        var inputtwo = $('#inputtwo').val();
        var multipliertwo = $('#multipliertwo').val();
        var totalTotalOne = (inputone * multiplierone);
        var totalTotalTwo = (inputtwo * multipliertwo);
        $('#TotalOne').text(totalTotalOne);
        $('#TotalTwo').text(totalTotalTwo);
    });
}); 

and updated jsFiddle: http://jsfiddle.net/eN7S6/4/

This little algorithm will get all the input.calc elements and multiply their values. Think this should work for you based on your given html.

var total = 1;
$("input.calc").val(function(idx, val) {
    total*= val;
})
$('#Total').text(total);

JSFiddle demo

There can never be two IDs Don the same page that are the same, that's why the class was invented.

Modify your code to something like this:

 <table>
   <tr>
     <td>
         <input name="inputone"class="inputone calc" value="1"><span id="Total"></span>
     </td>
   </tr>
   <tr>
     <td>
       <input name="inputone" class="inputone calc" value="1"><span id="Total"></span>
     </td>
   </tr>
 </table>
 <input name="inputtwo" class="calc inputtwo" value="2" type="hidden" readonly>
 <input name="inputtwo" class="calc inputtwo" value="3" type="hidden" readonly>
 <input type="button" id="update" value="Calculate" />

Then:

$(document).ready(function() {      
    $('#update').click(function() { 
        var inputone = $('.inputone').val();
        var inputtwo = $('.inputtwo').val();
        var totalTotal = ((inputone * 1) * (inputtwo * 1));     
        $('#Total').text(totalTotal);
    });
}); 

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