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"> </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;
}
答案 0 :(得分:1)
基本理念。选择元素并循环遍历它们。
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">