Uncaught ReferenceError: $ is not defined in shopping cart

0

Why does the Uncaught ReferenceError: $ is not defined error occur in the code below?

$(function funcao(){
    $(document).on('keydown', '#busca', function(){
        console.log('I\'m working');
    });
});

HTML

                      Search Cart              

<body>
    <form action="" method="post" enctype="multipart/form-data" id="form_busca">
        <label>
            <span>Buscar Produto</span>
            <input type="text" name="buscar" id="busca" />
        </label>
    </form>

    <div id="resultado_busca"></div>

    <form action="" method="post" enctype="multipart/form-data">
        <table border="0" cellpadding="0" cellspacing="0" width="80%">
            <thead>
                <tr>
                    <td>Produto</td>
                    <td>Valor</td>
                    <td>Qtd</td>
                    <td>Subtotal</td>
                </tr>
            </thead>

            <tbody id="content_retorno">
                <?php
                $total = 0;
                if(count($_SESSION['carrinho']) > 0):
                foreach($_SESSION['carrinho'] as $idProd => $qtd){
                    $pegaProduto = $pdo->prepare("SELECT * FROM 'produtos' WHERE 'id' = ?");
                    $pegaProduto->execute(array($idProd));
                    $dadosProduto = $pegaProduto->fetchObject();
                    $subTotal = ($dadosProduto->valor*$qtd);
                    $total += $subTotal;

                    echo '<tr><td>'.utf8_encode($dadosProduto->titulo).'</td><td>Valor</td><td><input type="text" id="qtd" value="'.$qtd.'" size="3" /></td>';
                    echo '<td>R$ '.number_format($subTotal, 2, ',', '.').'</td></tr>';

                }
                echo '<tr><td colspan="3">Total</td><td id="total">R$ '.number_format($total, 2, ',','.').'</td></tr>';
                endif;
                ?>
            </tbody>
        </table>
        <input type="submit" value="Concluir compra" class="botao" />
    </form>
    <script lang="javascript" src="jquery.js"></script>
    <script lang="javascript" src="functions.js"></script>
</body>

    
asked by anonymous 28.12.2016 / 03:06

1 answer

4

The use of jQuery is wrong. The $ (document) .ready () function has a similar signature.

// 1º Forma completa
$( document ).ready(function() {
    $(document).on('keydown', '#busca', function(){
        console.log('I\'m working');
    });
});

// 2º Forma - Reduzida
$(function() {
    $(document).on('keydown', '#busca', function(){
        console.log('I\'m working');
    });
});

// 3º Forma - Passando uma função nomeada
function readyFn( jQuery ) {
    $(document).on('keydown', '#busca', function(){
        console.log('I\'m working');
    });
}

$( document ).ready( readyFn );

When you run your code, your function conflicts with jQuery generating the error that is being presented.

If you need to define a new function for jQuery, see 5 ways to define a function

    
28.12.2016 / 03:36