我有一个表格,可以“坐在”表格中

                <form id="form">
            <input type="submit" value="send" class="btn btn-w-m btn-primary" style="float: left;">Add transaction</input>
            <table class="table table-striped table-bordered table-hover " id="editable" >
                <thead>
                <tr>
                    <th>Date</th>
                    <th>Name<br>(Last name, Firstname,<br>
                        or Business name)
                    </th>
                    <th>Address</th>
                    <th>Phone</th>
                    <th>Price with VAT</th>
                    <th>VAT</th>
                    <th>Transaction type</th>
                    <th>Currency</th>
                    <th>Installments</th>
                </tr>
                </thead>
                <tbody>
                <tr>
                    <td><input type="date" name="date"/></td>
                    <td><input type="text" name="name"/></td>
                    <td><input type="text" name="address"/></td>
                    <td><input type="text" name="phone"/></td>
                    <td><input type="text" name="price_with_vat"/></td>
                    <td>25%(from database)</td>
                    <td class="exclude"><select name="transaction_type">
                            <option>value1</option>
                            <option>value2</option>
                            <option>value3</option>
                        </select></td>
                    <td class="exclude"><select name="currency">
                            <option>Euro</option>
                            <option>USD</option>
                            <option>Pound</option>
                        </select></td>
                    <td class="exclude"><select name="installments">
                            <option>Yes</option>
                            <option>No</option>
                        </select></td>
                </tr>
                </tbody>


我想要的是选择所有输入值,并将Ajax请求发送到php端。问题是在我的jquery函数(以下代码)中,我无法收集所有输入。另外,尽管我有一个preventDefault页面仍然刷新。

    var request;
$('#form').submit(function(event){

    if (request){
        request.abort();
    }

    var form = $(this)

    var $inputs = $form.find("input, select, button, textarea");
    var serializedData = $form.serialize();
    $inputs.prop("disabled", true);
    console.log(serializedData);
    event.preventDefault();
});

最佳答案

试试这个代码


  对于要包含在序列化字符串中的表单元素的值,
  元素必须具有名称属性。


$(document).ready(function() {
  //Form submit event
  $('#form').on('submit', function(e){

    // validation code here
    if(!valid) {
      e.preventDefault();
    }

    //Serialized data
    var datastring = $("#form").serialize();

    //Ajax request to send data to server
    $.ajax({
        type: "POST",
        url: "your url.php",
        data: datastring,
        success: function(data) {
            alert('Data send');
        }
    });
  });
});

10-05 22:35