如何通过单击按钮捕获表单的数据?

我想通过单击按钮捕获表单的数据,并使用ajax、json将其发送到php页面。 现在,当我注释了AJAX部分时,单击按钮便会发出警报“ hello hello”。 当我通过取消包括ajax部分的注释时,不仅没有得到任何错误,而且“ hello hello”警报也没有出现。 我觉得这很奇怪——为什么会这样呢?

这是我的ajax:

<script>
    $(document).ready(function () {
        $("#btn").click( function() {
        alert('hello hello');
        /* Coomenting starts here
        $.ajax({
            url: "connection.php",
            type: "POST",
            data: {
                id: $('#id').val(),
                name: $('#name').val(),
                Address: $('#Address').val()
            }
            datatype: "json",
            success: function (status)
            {
                if (status.success == false)
                {
                    alert("Failure!");
                }
                else 
                {
                    alert("Success!");
                }
            }
        });
        */
    //commenting ends here.
    //basically i just commented out the ajax portion 
    });
    });
</script> 

Check comma syntax after data parameter:

data: {
         id: $('#id').val(),
         name: $('#name').val(),
          Address: $('#Address').val()
      },

You are missing a comma after "data":

$.ajax({
    url: "connection.php",
    type: "POST",
    data: {
        id: $('#id').val(),
        name: $('#name').val(),
        Address: $('#Address').val()
    }, // Missing comma!
    datatype: "json",
    success: function (status)
    {
        if (status.success == false)
        {
            alert("Failure!");
        }
        else 
        {
            alert("Success!");
        }
    }
});

As @dystroy pointed out, since your code can't compile, this malformed block will potentially prevent other code (such as a simple alert) from firing.

You are missing a comma after the data part:

<script>
    $(document).ready(function () {
        $("#btn").click( function() {
        alert('hello hello');
        /* Coomenting starts here
        $.ajax({
            url: "connection.php",
            type: "POST",
            data: {
                id: $('#id').val(),
                name: $('#name').val(),
                Address: $('#Address').val()
            },
            datatype: "json",
            success: function (status)
            {
                if (status.success == false)
                {
                    alert("Failure!");
                }
                else 
                {
                    alert("Success!");
                }
            }
        });
        */
    //commenting ends here.
    //basically i just commented out the ajax portion 
    });
    });
</script>