jQuery beforeSend没有看到元素

时间:2014-04-19 17:47:46

标签: jquery ajax

我有这个HTML:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>UC</title>
        <!-- css -->

        <!-- jQuery -->
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
        <!-- scripts -->
        <script src="<?php echo URL; ?>public/js/application.js"></script>
    </head>
    <body>
    <div id="form">form</div>
    </body>
</html>

和这个JS:

var URL = "https://192.168.2.112/pm-nc/ajax"
$(document).ready(preparePage());

function preparePage(){
    //check if user is logged in
    $.ajax({
        type: "POST",
        data: {
            request: "loginCheck"   
        },
        url: URL,
        dataType: "html",
        beforeSend: function(){
            alert($("#form").length);
        },
        success: function(data){
            $("#form").html(data);
        }
    });
}

我正在尝试将一些加载gif插入到带有id形式的元素中,但看起来当脚本启动时,id形式的元素还没有存在。警报显示0.任何想法有什么问题?

1 个答案:

答案 0 :(得分:0)

您需要ID选择器$('#form')。注意#符号。 $('form')表示“查找所有表单元素”。