JQuery obtains form form data and data submission (1)

Time:2022-10-7

HTML

<form id="form-box">
  ...
</form>

JQuery get form data

JQuery to get form data:

let json = $('#form-box').serialize();
console.log('json: ', json); // output: json: name=w3h5&type=web

Object format:

let data = {};
let value = $('#form-box').serializeArray();
$.each(value, function (index, item) {
  data[item.name] = item.value;
});
let json = JSON.stringify(data);
console.log(json);
/*
* output: {"name":"asd","type":"1"}
*/

JQuery form form submission

$("#form-box").submit();

ajax async submit

$.ajax({
  type: "POST",
  url: "/post.php",
  data: json,
  dataType : "json",
  success: function(respMsg){
  }
});