表单提交不要调用ajax函数phonegap

时间:2016-03-03 18:25:43

标签: php jquery ajax cordova

我正在开发一个带有phonegap的移动应用程序,我有一个form来注册新用户。在broswer(chrome和firefox)中,表单正常时工作正常 填写,如果出现问题则显示错误。我在函数的开头有一个alert('work!');来检查是否被调用,在浏览器中出现警报但是在app no中。什么可以? Ps:我已经有<access origin="">到我的网站,那里是php文件的形式。

修改

形式:

<form id="cadastro" method="post">

                <input type="text" style="height:30px;margin-bottom:10px;margin-top:10px;font-size:18px" name="nome" id="textinput-2" placeholder="Nome" required>   
                <input type="text" style="height:30px;margin-bottom:10px;font-size:18px" name="email" id="textinput-1" placeholder="Email" required>
                <div id="email_erro"></div>

                <input type="password" style="height:30px;margin-bottom:10px;font-size:18px" name="senha" id="password" placeholder="Senha" autocomplete="off" required>
                <input type="password" style="height:30px;margin-bottom:10px;font-size:18px" name="repetir_senha" id="re_password" placeholder="Repetir senha" autocomplete="off">
                <div id="senha_erro"></div>
                <fieldset data-role="controlgroup" style="margin-left:5%" data-type="horizontal" data-mini="true">
                            <input type="radio" name="sexo" id="radio-choice-c" value="m" checked="checked" required>
                            <label for="radio-choice-c">Masculino</label>
                            <input type="radio" name="sexo" id="radio-choice-d" value="f" required>
                            <label for="radio-choice-d">Feminino</label>
                </fieldset>       
                <div style="margin-left:3.5%;margin-right:4%;">
                    <input type="checkbox" name="newsletter" id="checkbox-mini-0" value="1" data-mini="true">
                        <label for="checkbox-mini-0">Quero receber as ofertas e novidades</label>
                    <input type="checkbox" name="termo" id="checkbox-mini-1" data-mini="true" required>
                        <label for="checkbox-mini-1">Aceito os Termos de Uso e Política de privacidade</label>
                    <button type="submit" class="ui-btn ui-shadow ui-corner-all" id="termo" style="float:none;margin-left:2%">Criar conta</button>
                </div>
            </form>

功能:

<script>
        alert('entrou no script');
        $('#cadastro').submit(function(){
            var valor = $('#cadastro').serialize();


    $.ajax({
    type: 'POST',
    url: 'cadastro',
    dataType: "json",
    data: valor

    }).success(function(response){
        alert('retornou função');
        if (response.sucesso) { 

            window.localStorage["email"] = response.dados.email;
            window.localStorage["nome"] = response.dados.nome;                             
            window.localStorage["sexo"] = response.dados.sexo;                             
            window.localStorage["id"] = response.dados.id; 
            //window.localStorage["UID"] = data.uid; 
            window.location = "logado.html";
        } else {
            alert("Verifique seus dados");

            $('#email_erro').html(response.erro_email);
            $('#senha_erro').html(response.erro_repetir_senha);

            //window.location("main.html");
            }

    });
    return false;
    });

    </script>

1 个答案:

答案 0 :(得分:0)

我相信你的问题是因为你在加载phonegap框架之前运行你的javascript。尝试使用下面的事件监听器,然后将您的javascript放在相应的函数中。

document.addEventListener("deviceReady", deviceReady, false);

function deviceReady() {
    // PhoneGap has loaded - your code goes here
}