我有这些jquery代码,这些代码将在用户聚焦时获取数据库中的数据,然后填充html输入。问题是当我有多行时,唯一被填充的行是第一行有帮助吗?请参见下面的代码。
$("input[id^='p_code']").focusin(function() {
var thisid = $(this).attr("id");
$(document).on('focusout', "input[id='" + thisid + "']", function() {
var prodappend = $("input[id='" + thisid + "']").val();
var prodprepend = padDigitss(prodappend, 8);
$("input[id='" + thisid + "']").val(prodprepend);
var searchVal = $(this).val();
if (searchVal !== '') {
var count1 = counts + 1;
$.get('functions/prodquery.php?page=purchaseorder&ctr=' + count1 + '&searchData=' + searchVal, function(returnData) {
if (!returnData) {
$('#showld').html('<p style="padding:5px;">Search term entered does not return any data.</p>');
} else {
$('#showld').html(returnData);
var prdname = $("input[id^='prdname']").val();
var prdunit = $("input[id^='prdunit']").val();
var prdunit2 = $("input[id^='produnit']").val();
var prdcost = $("input[id^='prdcost']").val();
var secondcost = $("input[id^='secondcost']").val();
var bcode = $("input[id^='bcode']").val();
$("input[id=barcode" + counts + "]").val(bcode);
$("input[id=units" + counts + "]").val(prdunit);
$("input[id=ucost" + counts + "]").val(prdcost);
$("input[id=p_name" + counts + "]").val(prdname);
$('select').append('<option value="' + prdcost + '">' + prdunit + '</option><option value="' + secondcost + '">' + prdunit2 + '</option>');
$("select[id=units" + counts + "]").change(function() {
var sval = $(this).val();
var tval = $("select[id=units" + counts + "]").find('option:selected').text();
$("input[id=utexts" + counts + "]").val(tval);
$("input[id=ucost" + counts + "]").val(sval);
var q_ty = $("input[id=qty" + counts + "]").val();
var amt = (parseFloat(q_ty) * parseFloat(sval));
$("input[id=amts" + counts + "]").val(amt);
});
$(document).on("focusout", "input[id=qty" + counts + "]", function() {
var q_ty = $("input[id=qty" + counts + "]").val();
var amts = (parseFloat(q_ty) * parseFloat(prdcost));
$("input[id=amts" + counts + "]").val(amts);
});
}
});
}
});
});
下面是我的HTML代码和PHP代码,带有文本框,当用户将焦点移出时,该文本框将被填充。
<table id="rredit" class="table table-bordered table-striped hover cell-border compact stripe">
<thead>
<tr>
<th width="10%" id="head">Bar Code</th>
<th width="10%" id="head">Product Code</th>
<th width="15%" id="head">Lot No.</th>
<th width="3%" id="head"></th>
<th width="9%" id="head">Expiry Date</th>
<th width="20%" id="head">Poduct Description</th>
<th width="8%" id="head">Qty</th>
<th width="10%" id="head">Unit</th>
<th width="20%" id="head">Cost</th>
<th width="20%" id="head">Amount</th>
<th width="10%" id="head">Delete</th>
</tr>
</thead>
<?php
while($rowrritem = sqlsrv_fetch_array($populatepoitem)){ $ctr= $ctr+1;?>
<tr>
<td><input size="14px" id="barcode<?php echo $ctr;?>" name="barcode[]" type="text" value="<?php echo $rowrritem['barcode']; ?> "></td>
<td><input size="6px" id="p_code<?php echo $ctr;?>" class="lew" name="p_code[]" type="text" value="<?php echo $rowrritem['product_code']; ?> "></td>
<td align="center"><input size="15px;" id="lot_no<?php echo $ctr;?>" name="lot_no[]" type="text" value="<?php echo $rowrritem['lot_no']; ?> "></td>
<td><a href="#ldModal" role="button" class="ld<?php echo $ctr;?>" id="<?php echo $rowrritem['product_code']; ?> " data-toggle="modal"><img src="images/new.png" width="30px" height="28px"></a></td>
<td align="center"><input size="6px" id="exp_date<?php echo $ctr;?>" name="exp_date[]" type="text" value="<?php if($rowrritem['exp_date'] !== null){ echo date_format($rowrritem['exp_date'], "Y-m-d");} ?> "></td>
<td><input id="p_name<?php echo $ctr;?>" disabled="" name="p_name[]" type="text" value="<?php echo $rowrritem['prod_name']; ?> "></td>
<td align="center"><input size="3px" id="qty<?php echo $ctr;?>" name="qty[]" type="text" value="<?php echo $rowrritem['qty']; ?> "></td>
<td align="center"><select name="unit[]" id="units<?php echo $ctr;?>"><option value="<?php echo $rowrritem['stdcost'];?>"><?php echo $rowrritem['unit1'];?></option><option value="<?php echo $rowrritem['stdcost2'];?>"><?php echo $rowrritem['unit2'];?></option></select></td>
<td align="center"><input size="4px" id="ucost<?php echo $ctr;?>" name="ucost[]" type="text" value="<?php echo $rowrritem['cost']; ?> "></td>
<td><input size="5px" id="amts<?php echo $ctr;?>" readonly name="amts[]" type="text" value="<?php echo $rowrritem['qty'] * $rowrritem['cost'];?>" ></td>
<td align="center"><a href="#" name="delete" id="del" class="btn btn-danger btn-sm">Delete</a></td>
</tr>