(function ($) {
$.fn.serializeFormJSON = function () {
var o = {};
var a = this.serializeArray();
$.each(a, function () {
if (o[this.name]) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
})(jQuery);
$('form').submit(function (e) {
e.preventDefault();
var data = $(this).serializeFormJSON();
console.log(data);
/* Object
email: "value"
name: "value"
password: "value"
*/}); or another approach
function jQFormSerializeArrToJson(formid) {
var serializedArr = $("form#" + formid).serializeArray();
var jsonObj = {};
jQuery.map(formSerializeArr, function(n, i) {
jsonObj[n.name] = n.value;
});
return jsonObj;
}
$('#formidlogin').submit(function(e) {
e.preventDefault();
var data = jQFormSerializeArrToJson('formidlogin');
data = JSON.stringify(data),
console.log(data);
/* Object
email: "value"
name: "value"
password: "value"
*/});
To collect a group of elements, we pass the selector to the jQuery function using…
jQuery is a lightweight, "write less, do more", JavaScript library. The purpose of jQuery is…
HTML:- [crayon-692f37106a0f5127053662/] CSS:- [crayon-692f37106a0f8873418626/] JavaScript:- [crayon-692f37106a0f9808909946/]
Prevent users typing special characters in text box, textarea, etc. [crayon-692f37106a19c755526806/] ^[a-zA-Z]+$ Alphabets ^[a-zA-Z\s]+$ Alphabets…
Any interactive web site has form input - a place where the users input different…
[crayon-692f37106a367210174524/]