在global.php中添加以下php代码以生成一个表
<table>
<tbody>
<?php
for($i=1; $i<=30; $i++)
{
echo '
<tr class="adding">
<td>'.$i.'</td>
<td><input type="text" class="barcode" name="barcode[]" size="13" maxlength="13"></td>
<td><input type="text" class="prodotto" name="prodotto[]"></td>
<td><input type="text" class="prezzo" name="prezzo[]"></td>
</tr>';
}
?>
</tbody>
</table>
<script src="js/global.js"></script>
这是我的global。js文件
$(document).on("blur", ".barcode", function(){
var barcode =$(this).find('input.barcode').val();
if(barcode!='')
{
$.post('ajax/barcode_prodotto.php', {name: barcode}, function(data){
$(this).find('input.prodotto').val(data);
});
}
});
,这是barcode_prodotto.php,我用它来检索数据从DB使用jquery post函数如下所述:
<?php
mysql_connect("localhost","root","");
mysql_select_db('test');
if(isset( $_POST['name'])===true && empty($_POST['name'])===false){
$query=mysql_query("
SELECT prodotto as prodotto
FROM barcode_prodotto
WHERE barcode_prodotto='".mysql_real_escape_string(trim($_POST['name']))."'");
echo (mysql_num_rows($query)!==0)? mysql_result($query, 0, 'prodotto') : 'prodotto not found';
}
?>
它不起作用,但我确信问题是在global.js文件上,因为我的其他2个php文件与id而不是类,一切都好
将您的global js
更改为:在其他变量中获取当前对象以在post ajax调用中使用,并找到td输入以将值放在里面。
$(document).on("blur", ".barcode", function(){
var that=this;
var barcode =$(this).val();
// console.log(barcode);
if(barcode!='')
{
$.post('testing.php', {name: barcode}, function(data){
$(that).closest("tr").children('td').slice(2,3).find("input").val(data);
});
}
});
演示