JQuery validates the form submission and serializes the form content

  • 2020-03-30 01:16:12
  • OfStack

The form submission method used in the previous project

Use the form() method to detach the submit event from the submit button and bind it to any event
 
function addSubmit(){ 
$('#addForm').form('submit', { 
url : _basePath + '/@Controller/@RequestMapping', 
onSubmit : function() { 
if(boolean){//Place a judgment condition on whether to commit
$.messager.show({ 
title:' prompt ',msg:' Does not meet preservation conditions ', 
showType:'fade',style:{right:'',bottom:''} 
}); 
return false;//Preventing form submission
} 
return $('#addForm').form('validate');//Determine whether all required entries have values
}, 
success : function(data) { 
var obj = jQuery.parseJSON(data);//Convert the returned JSON to the required object (ResponseData)
if (!obj.success) {//Determines the attribute value in the ResponseData object returned to indicate the state
$.messager.show({ 
title:' prompt ',msg:' Save failed ', 
showType:'fade',style:{right:'',bottom:''} 
}); 
} else { 
$.messager.show({ 
title:' prompt ',msg:' Save success ', 
showType:'fade',style:{right:'',bottom:''} 
}); 
$("#addWin").window("close");//Close the commit PWKK
query();//Refresh result set
} 
} 
}); 
} 

Today, in sharp jQuery, I read about another submission that USES ajax to encapsulate form content and post it
 
$("#btn").click(function() { 
$.get("get.php", {username:$("#username").val(), password:$("#password").val()}, function(data, textStatus) {//The data in the form is taken out one by one and then encapsulated and uploaded
$("#target").html(data);//Populates the return value into the page
}); 
}); 

Then there is a simplified version, serialized using the serialize() method
 
$("#btn").click(function() { 
$.get("get.php", $("#form").serialize(), function(data, textStatus) {//The data in the form is taken out one by one and then encapsulated and uploaded
$("#target").html(data);//Populates the return value into the page
}); 
}); 

The serialize() method can be coded automatically, and objects other than forms, such as the checkbox, can be converted using it

There is also the serializeArray() method, which serializes the element and returns a JSON object as an array instead of a JSON string

That is, you do not need to use the jquery.parsejson () method for conversion

The return value can be manipulated directly using methods such as $.each()

Related articles: