简体   繁体   English

当添加不同的值时,如何使用jQuery来添加分段的表单字段并进行更新?

[英]How can i use jQuery to add up sectioned form fields and update when different values are added?

In my form i have sectioned form fields (section 1, 2 etc...) for each form field i add up the values and display the total in the final field of each section, however if a user enters say 5 and then removes it and enters 0, the 5 is still added in the final score and i am unsure of how to allow changes to values so that they get rid of the old value. 在我的表单中,我为每个表单字段划分了多个表单字段(1、2等),我将这些值相加并在每个部分的最后一个字段中显示总计,但是如果用户输入“ 5”然后将其删除并输入0,则5仍会添加到最终分数中,我不确定如何允许更改值以使它们摆脱旧值。

The current way is: 当前方式是:

User adds value 5;
5 += final section field total;
User removes 5, instead puts 0;
final section field total = 5;

the code i am using is below 我正在使用的代码如下

var form_inputs = document.getElementsByTagName("input");

$.each(form_inputs, function(i){
    if($(form_inputs[i]).attr("type") == "hidden"){ return false; }
        $(form_inputs[i]).attr("type", "number");
        $(form_inputs[i]).attr("required", "true");
        $(form_inputs[i]).change(function(){
            if(i === 0 || i < 6){

                if(check_valid_pt($(this).uniqueId(), $(this).val())){
                    addValues(attempt, parseInt($(this).val()), i);
                } else {
                    alert("Please only enter 0, 4 or 5");
                    $(this).val("");
                }

            } else if(i > 20 && i < 39){

                if(check_valid_sr($(this).uniqueId(), $(this).val())){
                    addValues(attempt, parseInt($(this).val()), i);
                } else {
                    alert("Please only enter 0 or 5");
                    $(this).val("");
                }

            } else if(i > 59 && i < 78){

                if(check_valid_wfr($(this).uniqueId(), $(this).val(), "")){
                    addValues(attempt, parseInt($(this).val()), i);
                } else {
                    alert("Please only enter 0 or 5");
                    $(this).val("");
                }

            } else if(i > 77 && i < 108){

                if(check_valid_wfr($(this).uniqueId(), "", $(this).val())){
                    addValues(attempt, parseInt($(this).val()), i);
                } else {
                    alert("Please only enter 0, 3 or 5");
                    $(this).val("");
                }

            } else if(i > 128 && i < 159){

                if(check_valid_er($(this).uniqueId(), $(this).val())){
                    addValues(attempt, parseInt($(this).val()), i);
                } else {
                    alert("Please only enter 0, 3 or 5");
                    $(this).val("");
                }

            }
        });
});

I believe you are looking for something like the below: 我相信您正在寻找类似以下的内容:

 $('.addMe').change(function () { $('.addMe').removeClass('hasError'); var ttl = 0; $('.addMe').each(function (i, e) { ttl = Number($(e).val()) + ttl; var validVals = $(this).data('valid-vals').split(','); if (validVals.length > 0 && $(this).val() != '' && $.inArray($(this).val(), validVals) === -1) { $(this).addClass('hasError'); $(this).val(validVals.join(', ') + ' only'); } }); $('#cost').html($('.hasError').length > 0 ? '' : ttl); }); $('.addMe').focus(function () { if ($(this).val().match(/only/i)) { $(this).val(''); $(this).removeClass('hasError'); } }); 
 .addMe { margin-bottom: 15px; } .hasError { border-style: solid; border-width: 1px; border-color: red; background-color: #FFCCCC; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="cost"></div> <br> <br> <input type="text" value="" class="addMe" data-valid-vals="0,4,5" />Only - 0, 4, 5 <br> <input type="text" value="" class="addMe" data-valid-vals="0,5" />Only - 0, 5 <br> <input type="text" value="" class="addMe" data-valid-vals="0,5" />Only - 0, 5 <br> <input type="text" value="" class="addMe" data-valid-vals="0,3,5" />Only - 0, 3, 5 <br> <input type="text" value="" class="addMe" data-valid-vals="0,3,5" />Only - 0, 3, 5 <br> 

暂无
暂无

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

相关问题 如何使用js计算分段表单中的输入表单字段 - How to use js to calculate input form fields in a sectioned form 如何告诉jquery mobile将类添加到listview中动态添加的表单字段? - How do I tell jquery mobile to add classes to dynamically added form fields in a listview? 如何使用jQuery添加具有不同值的不同元素 - How can I add different elements with different values with jquery 如何添加动态添加的字段的值 - How to add values of dyanamically added fields 如何使用Bootstrap模态使用jQuery向表单添加字段 - How to use a Bootstrap modal to add fields to a form using jQuery 使用 jQuery,如何使用“实时”方法在添加新元素时发出警报? - With jQuery, how can I use the “live” method to alert when a new element is added? 如何使用javascript / jQuery获取表单字段值以求和另一个字段值? - How can I get form field values to sum up to another field value using javascript/jQuery? 如何将值从表单添加到 div,堆叠值? - How can I add value from form to div, stacking up the values? 如果可以动态添加或删除输入字段,如何按键对从 HTML 表单发布的值进行分组 - How to group values posted from HTML form by key if input fields can be dynamically added or deleted CRM 2011批量编辑表单:如何为每个记录记录更新具有不同值的某些表单字段? - CRM 2011 Bulk Edit form : how to update some of the form fields with different values for each record record?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM