Javascript代码从未加载

Javascript代码从未加载,javascript,php,html,Javascript,Php,Html,我有一个javascript代码,我已经硬编码到我的html文件中,但它似乎从未加载。我的表单通过post立即将数据发送到php文件,而无需每次进行javascript验证。有什么想法吗?谢谢 <!DOCTYPE html> <html> <head> <!-- Stylesheets - boostrap and get-shit-done, a free UI --> <link rel="sty

我有一个javascript代码,我已经硬编码到我的html文件中,但它似乎从未加载。我的表单通过post立即将数据发送到php文件,而无需每次进行javascript验证。有什么想法吗?谢谢

<!DOCTYPE html>
<html>
    <head>

        <!-- Stylesheets - boostrap and get-shit-done, a free UI -->
        <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
        <link rel="styelsheet" type="text/css" href="css/get-shit-done.css">
        <link rel="stylesheet" type="text/css" href="css/styles.css">

        <h1>Registration</h1>

        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

        <script>
        $('#registration').on('submit', function(e){
            var usernameValue = $("#username").val();
            var passwordValue = $("#password").val();
            var confirmationValue = $("#confirmation").val();

            // Returns successful data submission message when the entered information is stored in database.
            var dataString = 'username1='+ usernameValue + '&password1='+ passwordValue + '&confirmation1='+ confirmationValue;
            if(usernameValue==''||passwordValue==''||confirmationValue=='') {
                alert("Please Fill All Fields");
            }
            else {
                // AJAX Code To Submit Form.
                $.ajax({
                    type: "POST",
                    url: "register.php",
                    data: dataString,
                    cache: false,
                    success: function(result){
                        alert(result);
                    }
                });
            }
            return false;
        });

        </script>

    </head>


    <body>



        <form id ="registration" action="register.php" method="post">
            <fieldset>
            <legend>Registration Details</legend>
                <form class="form-inline">
                    <div class="form-group">
                        <input autocomplete="off" autofocus class="form-control" id="username" placeholder="Username" type="text"/>
                    </div>
                    <div class="form-group">
                        <input class="form-control" id="password" placeholder="Password" type="password"/>
                    </div>
                    <div class="form-group">
                        <input class="form-control" id="confirmation" placeholder="Password" type="password"/>
                    </div>
                    <div class="form-group">
                        <button class="btn btn-default btn-round" type="submit" id="submit">
                            <span aria-hidden="true" class="glyphicon glyphicon-log-in"></span>
                            Log In
                        </button>
                    </div>
                </form>
            </fieldset>
        </form>

        <div>
            <p class="text-center">or <a href="welcome.php">login</a></p>
        </div>

</body>
</html>

登记处
$(“#注册”)。关于('submit',函数(e){
var usernameValue=$(“#username”).val();
var passwordValue=$(“#password”).val();
var确认值=$(“#确认”).val();
//当输入的信息存储在数据库中时,返回成功的数据提交消息。
var dataString='username1='+usernameValue+'&password1='+passwordValue+'&confirmation1='+confirmationValue;
如果(用户名值=“”| |密码值=“”| |确认值=“”){
警告(“请填写所有字段”);
}
否则{
//提交表单的AJAX代码。
$.ajax({
类型:“POST”,
url:“register.php”,
数据:dataString,
cache:false,
成功:功能(结果){
警报(结果);
}
});
}
返回false;
});
注册详情
登录


您需要在('submit',function(e){})上包装您的
$('#registration')
$(函数(){})中使用
(或
$(document).ready()
)因为它在文档准备就绪之前当前处于绑定状态-

<script>
$(function(){    
    $('#registration').on('submit', function(e){
        ...
        [rest of your code]
        ...
    });
});
</script>

$(函数(){
$(“#注册”)。关于('submit',函数(e){
...
[代码的其余部分]
...
});
});

请尝试在jquery文档中添加脚本。范例



你不需要行动和方法。。删除您是否尝试在提交调用的开头添加preventDefault()函数?您需要在('submit',函数(e){…})上包装
$('#registration')
$(函数(){…})中
@jfriend00这是在
中,因此它位于
之前。。。
    $( document ).ready(function() {
       $('#registration').on('submit', function(e){
        var usernameValue = $("#username").val();
        var passwordValue = $("#password").val();
        var confirmationValue = $("#confirmation").val();

        // Returns successful data submission message when the entered information is stored in database.
        var dataString = 'username1='+ usernameValue + '&password1='+ passwordValue + '&confirmation1='+ confirmationValue;
        if(usernameValue==''||passwordValue==''||confirmationValue=='') {
            alert("Please Fill All Fields");
        }
        else {
            // AJAX Code To Submit Form.
            $.ajax({
                type: "POST",
                url: "register.php",
                data: dataString,
                cache: false,
                success: function(result){
                    alert(result);
                }
            });
        }
        return false;
      });
    });