Jquery表单提交不起作用

时间:2018-03-22 19:43:17

标签: jquery html forms

我试图提交一个简单的表单,但#createVMForm日志的结果(如下面的代码所示)是空的。

但我可以在浏览器网址栏中看到这些值......

<form id="createVMForm" action="">
    <div class="form-group">
        <label for="username">Username:</label>
        <input type="text" class="form-control" name="username" id="username" value= "hi" required="true">
    </div>
    <div class="form-group">
        <label for="email">User Email:</label>
        <input type="email" class="form-control" name="email" id="email" required="true">
    </div>
    <div class="form-group">
        <label for="imageName">Image Name:</label>
        <input type="text" class="form-control" name="imageName" id="imageName" required="true">
    </div>
    <div class="form-group">
        <label for="memory">DC Memory (in MB):</label>
        <input type="number" class="form-control" name="memory" id="memory" required="true">
    </div>
    <div class="form-group">
        <label for="vcpu">Number of VCPUs:</label>
        <input type="number" class="form-control" name="vcpu" id="vcpu" required="true">
    </div>
    <div class="form-group">
        <label for="vncUsername">VNC Username:</label>
        <input type="text" class="form-control" name="vncUsername" id="vncUsername" required="true">
    </div>
    <div class="form-group">
        <label for="vncPwd">VNC password:</label>
        <input type="password" class="form-control" name="vncPwd" id="vncPwd" required="true">
    </div>
    <button type="submit" class="btn btn-success">Create DC</button>
</form>

$('#createVMForm').submit(function() {
    console.log($('#createVMForm').serializeArray());
});

1 个答案:

答案 0 :(得分:0)

您的表单默认提交整个页面

改变这个:

    $('#createVMForm').submit(function() {
         console.log($('#createVMForm').serializeArray());
     });

对此:

    $('#createVMForm').submit(function(e) {
         e.preventDefault();
         console.log($('#createVMForm').serializeArray());
     });

这将阻止表单提交页面并允许您在jQuery块中执行操作。