简体   繁体   English

Javascript添加变量和更改事件

[英]Javascript Add Variable and change event

I have the following code 我有以下代码

$(document).ready(function () {
    $('#big_1').change(function () {
        var bigAmt = document.getElementById("big_1").value
            + document.getElementById("big_2").value
            + document.getElementById("big_3").value
            + document.getElementById("big_4").value
            + document.getElementById("big_5").value
            + document.getElementById("big_6").value
            + document.getElementById("big_7").value 
            + document.getElementById("big_8").value
            + document.getElementById("big_9").value
            + document.getElementById("big_10").value;

        var elem = document.getElementById("totalBig");
        elem.value = bigAmt;
    });
});

I actually wanted to add the value of big_1 to big_10 on input text value change of "big_1 to big_10" either 1 of the textfield change its value, this should be invoke. 我实际上想在输入文本值更改“ big_1 to big_10”中将big_1的值添加到big_10,或者任一textfield更改其值,都应调用此方法。

as of now i only run on big_1 change event. 截至目前,我仅在big_1 change事件上运行。

I get an javascript error by adding this way, I think the way I add them up is quite messy. 通过添加这种方式我收到了一个JavaScript错误,我认为将它们加起来的方式相当混乱。

What should I do to change my code so I can sum up 我应该怎么做才能更改代码,以便总结一下

big_1 to big_10 textfield value, and on change of big_1 to big_10(any of them), it will invoke this and change span id="totalBig" to the value of their sum (big_1 add until big_10) 将big_1更改为big_10文本字段值,并将big_1更改为big_10(其中任何一个),它将调用此方法并将span id =“ totalBig”更改为其和值(big_1加到big_10)

Below is my edited extra code: 以下是我编辑的额外代码:

<input type="number" data-bv-digits-message="true" data-bv-threshold="1" min="0" class="form-control" name="big_1" id="big_1" size="6">

<input type="number" data-bv-digits-message="true" data-bv-threshold="1" min="0" class="form-control" name="big_2" id="big_2" size="6">


all the way until big_10

I wanna on change value of any of this big_Identifier(1-10), it will sum it up and change my 我想更改任何big_Identifier(1-10)的值,它将汇总并更改我的

<div class="well">
Total Big: <span id="totalbig">0</span> &nbsp;&nbsp;&nbsp;&nbsp;</span> 
</div>

I tried the 我尝试了

<script type="text/javascript">
$(document).ready(function() {
$('#html5Form').bootstrapValidator();
    $('.big').change(function() { 
        var bigAmt = "";

        $('.big').each(function () {
            bigAmt += $(this).val();    
        })

        var elem = document.getElementById("totalBig");
alert(bigAmt);
        elem.value = bigAmt;
    });
});
</script>

It doesn't run any alert when any of the big_ value was changed. 更改任何big_值时,它都不会运行任何警报。

It would be much better if you added a big class to every single <input id="big_NUMBER"> . 这将是好得多,如果你增加了一个big类的每一个<input id="big_NUMBER"> Then you could do this: 然后,您可以这样做:

$(document).ready(function() {
    $('.big').change(function() { 
        var bigAmt = 0;

        $('.big').each(function () {
            bigAmt += Number($(this).val());    
        })

        $("#totalBig").val(bigAmt);
    });
});

That's much cleaner and easier to understand than what you had. 这比您拥有的东西更清洁,更容易理解。

In order for this to work, you'll need to add a class to all your inputs: 为了使它起作用,您需要在所有输入中添加一个类:

<input type="number" data-bv-digits-message="true" data-bv-threshold="1" min="0" class="form-control big" name="big_2" id="big_2" size="6"><!-- Notice the big class-->

This is the best way to group all your inputs. 这是对所有输入进行分组的最佳方法。 They are all related, so they should share a classes. 它们都是相关的,因此应该共享一个类。 You should not be calling multiple ids for functionality that's so similar. 您不应为如此相似的功能调用多个ID。

If you are using jquery, use it properly, it'll make your life a lot easier. 如果您使用的是jquery,请正确使用它,这将使您的生活更加轻松。

This will work for you in your case exactly 这将完全适合您的情况

$(document).ready(function() {
    $('[id^="big"').change(function(){
        var total = (+$('#totalBig').val());
        var currentVal = (+$(this).val());
        total += currentVal;
        $('#totalBig').val(total)
    })
});

DEMO DEMO

You monitor the change event on all the input type text as follows: 您可以按以下方式监视所有输入类型文本上的更改事件:

$('input:text').change(
function () {
    alert('text changed of any text box.');
    //You can doo your code here.
});

Or... 要么...

If you want add the monitor to any selected text boxes then you will have to add any css class to those selected text boxes and then monitor those text boxes through class as follows: 如果要将监视器添加到任何选定的文本框中,则必须将所有css类添加到那些选定的文本框中,然后通过class监视这些文本框,如下所示:

$('.yourclass').change(
function () {
    alert('text changed of any text box.');
    //You can doo your code here.
});

this change event will fire when you lose focus from the text box after changing the text.... 当您在更改文本后从文本框中失去焦点时,将触发此更改事件。

but if you want with loosing the focus (means if you want to update the count while typing) then you should use keyup event as stated in this answer. 但是,如果您想松开焦点(意味着如果您想在键入时更新计数),则应使用答案中所述的keyup事件。

$(document).ready(function() {
    $('#big_1').change(function() {
        var divArray = ["big_1","big_2","big_3","big_4","big_5","big_6","big_7","big_8","big_9","big_9","big_10"];
        var bigAmt = 0;

        for(var i = 0, n = divArray.length;i<n;i++)
        {
            bigAmt += parseInt($("#" + divArray[i]).val(),10);
        }   
        $("#totalBig").val(bigAmt);
    });
});

Try the above, it should do what you're looking for. 尝试上面的方法,它应该可以满足您的需求。 You'll probably want to use parseInt as well incase the input isn't of "number" type. 如果输入不是“数字”类型,您可能还需要使用parseInt。

*edit, forgot the # for the id. *编辑,忘记了ID号。

*edit, removed comment about considering using jquery functions because people are really sensitive. * edit,删除了关于考虑使用jquery函数的注释,因为人们确实很敏感。

Add class="bigs" to all inputs and then try this: class="bigs"添加到所有输入,然后尝试以下操作:

$(document).ready(function () {
    var intTotalBig;
    $('.bigs').change(function () {
        intTotalBig = 0;
        $('.bigs').each(function(){
            $thisVal = $(this).val();
            if ($.isNumeric($thisVal)){
                intTotalBig += parseInt($thisVal, 10);
            }
        });
        $("#totalBig").val(intTotalBig);
    });
});

This code check all inputs on every change and sum all of them that has a number value and ignore empty or no number values. 该代码检查每次更改的所有输入,并对所有具有数字值的输入求和,并忽略空值或无数字值。

Check JSFiddle Demo 查看JSFiddle演示

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

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