简体   繁体   English

使用jQuery相乘并添加2个输入框

[英]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. 脚本几乎可以做到这一点,但只能用于1个输入。

Any easy way to do it? 有任何简单的方法吗?

Much appreciated. 非常感激。

Fiddle: http://jsfiddle.net/eN7S6/3/ 小提琴: 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 和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. 您的元素ID不是唯一的。 You have duplicates, such as "inputone" and "inputtwo". 您有重复项,例如“ inputone”和“ 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: 和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/ 并更新了jsFiddle: http : //jsfiddle.net/eN7S6/4/

This little algorithm will get all the input.calc elements and multiply their values. 这个小算法将获取所有input.calc元素并将它们的值相乘。 Think this should work for you based on your given html. 认为这应该基于给定的html为您工作。

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

JSFiddle demo JSFiddle演示

There can never be two IDs Don the same page that are the same, that's why the class was invented. 永远不会有两个IDs在同一个页面上是相同的,这就是发明class的原因。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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