jQuery click () is not running [closed]

-2

I'm trying to do a function in jQuery that by clicking on the button it executes the click () function of jQuery to pick up input data and send via post

HTML

<input type="text" name="nome" class="input-xlarge" placeholder="Nome" required="required" />
<input type="email" name="email"  class="input-xlarge" placeholder="Endereço de email" required="required" />
<input type="text" name="dominio" class="input-xlarge" placeholder="Domínio" required="required" />
<button type="submit" class="btn submit" id="submit">Criar uma conta</button>

jQuery

<script>
    $(document).ready(function()
    {
        $("#submit").click(function()
        {
            var nome = $("input[name=nome]").val();
            var email = $("input[name=email]").val();
            var dominio = $("input[name=dominio]").val();
            $.post('api.php', {nome:nome, email:email}, function(data)
            {   
                alert(data);    
            })
        })
    })
</script>

An error appears in the browser console:

  

Uncaught ReferenceError: Datex is not defined preco.php: 29 (anonymous   function) preco.php: 29 (anonymous function) preco.php: 30 Uncaught   ReferenceError: _gaq is not defined global.js: 1 (anonymous function)   global.js: 1 jquery.min.js: 2 p.fireWith jquery.min.js: 2   e.extend.ready jquery.min.js: 2 c.addEventListener.B

    
asked by anonymous 03.07.2014 / 04:28

1 answer

4

Try this code:

$(document).ready(function(){

    $("#submit").click(function(event){

        event.stopPropagation();

        var nome = $("input[name=nome]").val();
        var email = $("input[name=email]").val();
        var dominio = $("input[name=dominio]").val();

        $.post('api.php', {nome:nome, email:email}, function(data){

            alert(data);
        });

        return false;
    });
});

The problem happens because your button has type="submit" . The browser understands that by clicking this button the user will be submitting information to the server.

Using event.stopPropagation() vc ensures that the event does not propagate.

The return false at the end of the function is required to stop processing the event ( read this )

    
03.07.2014 / 04:39