与美元提交表单。提交按钮在表单之外时使用ajax

时间:2022-04-09 08:38:15

i have multiple form, so i create a form with button submit outside form, this form

我有多个表单,所以我创建了一个表单,在表单外提交按钮

$("#buttonSubmit").click(function (event) {
            event.preventDefault();
            if (confirm("Anda yakin akan Checkout ?")) {
                var formData = new FormData("form#formData");
                $(".loader").show();
                $.ajax({
                    url: 'belanja/belanja_crud.php',
                    type: 'POST',
                    data: formData,
                    async: false,
                    cache: false,
                    contentType: false,
                    processData: false,
                    dataType: 'json',
                    success: function (data) {
                       //console.log(data);
                    }
                });
            }
            return false;
        });
<form id='formData'>
  <!-- input bla bla bla -->
  </form>
<button id='buttonSubmit' type='button'>Submit</button>

how to get all input from form on above and submit with ajax ?

如何从上面的表单中获取所有输入并使用ajax提交?

1 个解决方案

#1


2  

To get all data in the form in one instruction you can use .serialize()

要在一条指令中获取表单中的所有数据,可以使用.serialize()

E.g

$.ajax({
    type: "POST",
    url: 'belanja/belanja_crud.php',
    data: $("#formData").serialize()
})
.done(function (data) {
    //do somthing
})
.fail(function (xhr, ajaxOptions, thrownError) {
    //do something
});

Or you can see this topic if you want it to JSON Format

如果想要JSON格式,也可以看到这个主题

#1


2  

To get all data in the form in one instruction you can use .serialize()

要在一条指令中获取表单中的所有数据,可以使用.serialize()

E.g

$.ajax({
    type: "POST",
    url: 'belanja/belanja_crud.php',
    data: $("#formData").serialize()
})
.done(function (data) {
    //do somthing
})
.fail(function (xhr, ajaxOptions, thrownError) {
    //do something
});

Or you can see this topic if you want it to JSON Format

如果想要JSON格式,也可以看到这个主题