Uncaught Referenceerror: $ is not defined in shopping cart

Asked

Viewed 838 times

0

Why the error occurs Uncaught ReferenceError: $ is not defined 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>

  • Put this function around your code: (function($){/* o teu código aqui... */})(jQuery);. Works?

1 answer

4

The use of jQuery is wrong. The function $(Document). ready() 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 running your code, its 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.