JQuery obtaining form data and data submission (I)

Time:2022-6-20

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 submission

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

Ajax asynchronous commit

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