Jquery Post不能使用表单类

时间:2015-10-04 12:05:08

标签: javascript php jquery

我在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,我正在使用jquery post函数从DB中检索数据,如下所述:

<?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个带有id而不是类的php文件,一切都还可以。

1 个答案:

答案 0 :(得分:0)

将你的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);
        });
    }
});

demo

相关问题