序列化表单以便通过Ajax发送它

时间:2018-01-07 10:28:47

标签: javascript jquery ajax forms

我想通过Ajax发送表单,而不是通过提交按钮。

我在下面的代码摘录中定义了如下表单,后来我定义了jQuery函数来捕获CREATE BUTTON操作。

当我调试这个时,我得到了

console.log($('#customer_form').serialize());不会抛出任何东西。

  • 这是序列化表单的正确方法吗?
  • 按钮是否需要位于<form></form>元素内?

以下是使用过的代码:

HTML

<form id="customer_form" role="form">

    <div class="form-group">
        <label class="col-sm-4 control-label" for="name">Name</label>
        <div class="col-sm-8">
            <input type="text" class="form-control" id="name" aria-describedby="name_help" placeholder="Name">
            <small id="name_help" class="form-text text-muted"></small>
        </div>
    </div>


    <div class="form-group">
        <label class="col-sm-4 control-label" for="address_line_1">Address</label>
        <div class="col-sm-8">
            <input type="text" class="form-control" id="address_line_1" aria-describedby="address_line_1_help" placeholder="Address Line 1">
            <small id="address_line_1_help" class="form-text text-muted"></small>
        </div>
    </div>


    <div class="form-group">
        <label class="col-sm-4 control-label" for="address_line_2"></label>
        <div class="col-sm-8">
            <input type="text" class="form-control" id="address_line_2" aria-describedby="address_line_2_help" placeholder="Address Line 2">
            <small id="address_line_2_help" class="form-text text-muted"></small>
        </div>
    </div>


    <div class="form-group">
        <label class="col-sm-4 control-label" for="town">Town</label>
        <div class="col-sm-8">
            <input type="text" class="form-control" id="town" aria-describedby="town_help" placeholder="Town">
            <small id="town_help" class="form-text text-muted"></small>
        </div>
    </div>

    <button id="create" class="btn btn-sm btn-primary pull-right m-t-n-xs" type="button"><strong>Save</strong></button>

</form>

的JavaScript

$(document).ready(function(){
    $('#create').click(function(event) {
        console.log('foo');
        alert($('#customer_form').serialize());
        event.preventDefault();
    });
});

1 个答案:

答案 0 :(得分:2)

您忘了在输入中添加名称属性:

<input name="nameofInput" .... />
-------^

请参阅以下代码段:

$(document).ready(function(){
      $('#create').click(function(event) {
          console.log('foo');
          alert($('#customer_form').serialize());
          event.preventDefault();
      });
  });
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="customer_form" role="form">

    <div class="form-group">
        <label class="col-sm-4 control-label" for="name">Name</label>
        <div class="col-sm-8">
            <input type="text" class="form-control" name="name" id="name" aria-describedby="name_help" placeholder="Name">
            <small id="name_help" class="form-text text-muted"></small>
        </div>
    </div>


    <div class="form-group">
        <label class="col-sm-4 control-label" for="address_line_1">Address</label>
        <div class="col-sm-8">
            <input type="text" class="form-control"  name="adress1" id="address_line_1" aria-describedby="address_line_1_help" placeholder="Address Line 1">
            <small id="address_line_1_help" class="form-text text-muted"></small>
        </div>
    </div>


    <div class="form-group">
        <label class="col-sm-4 control-label" for="address_line_2"></label>
        <div class="col-sm-8">
            <input type="text" class="form-control" name="adress2" id="address_line_2" aria-describedby="address_line_2_help" placeholder="Address Line 2">
            <small id="address_line_2_help" class="form-text text-muted"></small>
        </div>
    </div>


    <div class="form-group">
        <label class="col-sm-4 control-label" for="town">Town</label>
        <div class="col-sm-8">
            <input type="text" class="form-control" name="town" id="town" aria-describedby="town_help" placeholder="Town">
            <small id="town_help" class="form-text text-muted"></small>
        </div>
    </div>

    <button id="create" class="btn btn-sm btn-primary pull-right m-t-n-xs" type="button"><strong>Save</strong></button>

</form>