javascript - 从具有相同名称的多个输入 jQuery 中获取值并将这些值添加在一起

标签 javascript jquery

Image of the appending of items using jQuery

function fill(thisValue) {
    function suggest(inputString) {
        var inputString = $.trim(inputString);
        if(inputString.length == 0) {
            $('#suggestions').fadeOut('slow');
        } else {
            $('#country').addClass('load');
            $.post("<?php echo base_url();?>customer/get_item_list", {queryString: "" + inputString + ""}, function(data) {
                if(data.length >0) {
                    $('#suggestions').fadeIn();
                    $('#suggestionsList').html(data);
                    $('#country').removeClass('load');
                } else {
                    $("#scan_box").val("");
                }
            });
        }
    }

    var MaxInputs = 15; //maximum input items allowed
    var x = 0;
    var FieldCount = 0; //to keep track of text box added

    function fill(thisValue) {
        setTimeout("$('#suggestions').fadeOut();", 1000);
        $("#scan_box").val("");
        data = thisValue.split('-');
        $('#item_code').val(data['0']);
        $('#item_name').val(data[1]).css('font-weight','bolder');
        $('#cost_price').val(data[2]);
        $('#item_code_code').val(data['0']);
   
        if(x <= MaxInputs) { //max input box allowed
            FieldCount++;
            $('#pos-tbbody').append('<tr id="tr" style="font-weight:bolder"><td>'+data[1]+'</td><td><input type="hidden" required id="item_code" name="item_code[]" value="'+data['0']+'"><input type="text" size="20" value="'+data[2]+'" style="width:100px" name="cost_price[]"></td><td><input type="number" class="qty" style="width:70px"  value="1" name="qty[]" onclick="multiply(this.value)" onkeydown="multiply(this.value)" onkeyup="multiply(this.value)" ></td> <td></td> <td class="text-center"><div class="btn-group btn-group-xs"><a href="javascript:void(0)" data-toggle="tooltip" title="Delete" class="btn btn-danger removeclass"><i class="fa fa-times"></i></a></div></td></tr>');
            x++;
        }
        return false;
    }

    function multiply(qty_value) {
        var total = data[2] * qty_value;
        //$('.total_amount').text(total);
        $("#amount_tendered").val(total);
        return total;
    }
    $(document).ready(function() {
        $('#pos-tbbody').on("click", ".removeclass", function(e) { //user click on remove text
            if(x >= 1) {
                $('#tr').remove();
                x--;
            }
            return false;
        });
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<div> 
    <table>
        <tr>
            <td><label class="control-label">Search</label></td>
            <td><input type="text" style="background-color:#EAEDF1;font-size:1.2em;font-weight:bolder" onkeyup="suggest(this.value);" onkeydown="suggest(this.value);" name="item_search" class="form-control" size="80"  placeholder="Start Typing item's or Scan barcode" id="scan_box" onblur="fill();"/></td>
        </tr>
    </table>
    <div class="col-md-9 suggestionsBox" id="suggestions" style="display:none; cursor:pointed;z-index:2000px ;display: block; position: absolute; ">
        <p class="suggestionList" id="suggestionsList" style="z-index:2000px ;cursor:pointed"> &nbsp; </p>
    </div>	
    <div class="table-responsive" style="height:22em;overflow:scroll">
        <table class="table table-vcenter table-hover table-striped table-bordered ">
            <thead style="padding:10px">
                <tr style="background-color:#563D7C;color:#fff;">
                    <th class="text-center" style="width: 150px;">Item </th>
                    <th>Price</th>
                    <th>Qty.</th>
                    <th>Total</th>
                    <th class="text-center">Actions</th>
                </tr>
            </thead>
            <tbody id="pos-tbbody">
            </tbody>
            </thead>
        </table>
    </div>
    <input type="submit" value="Save" class="btn btn-success btn-block btn-sm btn-raised" name="save_item_recieve">				
</div>

setTimeout("$('#suggestions').fadeOut();", 1000); 

$("#scan_box").val("");
data = thisValue.split('-');  

$('#item_code').val(data['0']);
$('#item_name').val(data[1]).css('font-weight','bolder');
$('#cost_price').val(data[2]);
$('#item_code_code').val(data['0']);    

if(x <= MaxInputs) //max input box allowed
{   
    FieldCount++; 

    $('#pos-tbbody').append('<tr id="tr" style="font-weight:bolder">

    <td>'+data[1]+'</td>

    <td><input type="hidden" required id="item_code" name="item_code[]" value="'+data['0']+'"><input type="text" size="20" value="'+data[2]+'" style="width:100px" name="cost_price[]"></td>

    <td><input type="number" class="qty" style="width:70px"  value="1" name="qty[]" onclick="multiply(this.value)" onkeydown="multiply(this.value)" onkeyup="multiply(this.value)" ></td> 

    <td></td> <td class="text-center"><div class="btn-group btn-group-xs"><a href="javascript:void(0)" data-toggle="tooltip" title="Delete" class="btn btn-danger removeclass"><i class="fa fa-times"></i></a></div></td>

    </tr>'); 

    x++;    
}
    return false;    

} 

最佳答案

基本思想。选择元素并循环它们。

function addUp(){
    var cnt = 0;  
    $("[name='theName']").each(function() {
        cnt += Number(this.value);
    });
    $("#total").val(cnt);
}
$("[name='theName']").on("change", addUp);
addUp();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" name="theName" value="1" />
<input type="text" name="theName" value="1" />
<input type="text" name="theName" value="1" />
<input type="text" name="theName" value="1" />
<input type="text" name="theName" value="1" />
<label for="total">Total:</label><input type="text" id="total">

关于javascript - 从具有相同名称的多个输入 jQuery 中获取值并将这些值添加在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33281690/

相关文章:

javascript - jQuery按钮点击,获取带有class的div

javascript - 从 N 到最后一个元素的切片数组

Javascript promise : Can I know which part of promise chain caused error?

javascript - JS support() 在 html 中显示标签

jQuery ajax 请求因跨源而被阻止

Javascript 代码已更改/主横幅无法正常工作

javascript - 使用 jQuery 向表格追加新行

javascript - 打印时缺少图像

jquery - JQPlot - 使 y 轴仅是整数?

javascript - JQuery - 通过获取 html 元素、单击属性来创建数组对象