Jquery Post不能使用表单类

Jquery Post不能使用表单类

问题描述:

Ive this php code in global.php to generate a table

<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>

and this is my global.js file

$(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);
        });
    }
});

and this is barcode_prodotto.php that I'm using to retrive data from DB using jquery post function as described:

<?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';
}
?>

It doesnt work but I'm sure that the problem is on global.js file because I the others 2 php files with id and not class and everything were ok.

Change your global js to this:get the current object in some other variable to use inside post ajax call and find the td input to place the value inside.

$(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