Home >Web Front-end >JS Tutorial >Implementation of verifying form submission method and serializing form content in jQuery_jquery

Implementation of verifying form submission method and serializing form content in jQuery_jquery

WBOY
WBOYOriginal
2016-05-16 17:05:151014browse

The form submission method used in previous projects

Use the form() method to separate the submission event from the submit button and bind it to any event

Copy code The code is as follows:

function addSubmit(){
$('#addForm').form('submit', {
url : _basePath '/@Controller/@RequestMapping',
onSubmit : function() {
if(boolean){//Place the judgment condition whether it can be submitted
$.messager.show({
title: 'Prompt',msg:'Does not meet saving conditions',
showType:'fade',style:{right:'',bottom:''}
});
return false;//Block Form submission
}
return $('#addForm').form('validate');//Judge whether all required items have values
},
success: function(data) {
var obj = jQuery.parseJSON(data);//Convert the returned JSON into the required object (ResponseData)
if (!obj.success) {//Judge the attribute value indicating the status in the returned ResponseData object
$.messager.show({
title:'Prompt',msg:'Save failed',
showType:'fade',style:{right:'',bottom:''}
});
} else {
$.messager.show({
title:'Prompt',msg:'Save successfully',
showType:'fade',style:{right: '',bottom:''}
});
$("#addWin").window("close");//Close submission pwkk
query();//Refresh the result set
}
}
});
}

Today I read another submission method in "Sharp jQuery", which uses ajax to encapsulate the form content. Post submission
Copy code The code is as follows:

$("#btn").click( function() {
$.get("get.php", {username:$("#username").val(), password:$("#password").val()}, function(data , textStatus) {//Remove the data from the form one by one and package and upload
$("#target").html(data);//Fill the return value into the page
});
});

Then there is a simplified version, using the serialize() method to serialize
Copy code The code is as follows:

$("#btn").click(function() {
$.get("get.php", $("#form").serialize( ), function(data, textStatus) {//Extract the data from the form one by one and then package and upload it
$("#target").html(data);//Fill the return value into the page
} );
});

The serialize() method can be automatically encoded, and objects other than forms such as checkboxes can also be converted using it

Also The serializeArray() method can serialize elements and return a JSON object in the form of an array. Instead of a JSON string

, there is no need to use the jQuery.parseJSON() method for conversion

The return value can be used directly Use methods similar to $.each() to operate
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn