Jquery. validate and jquery. form are used together to implement AJAX submission after verification form, jqueryvalidateajax

Source: Internet
Author: User

Jquery. validate and jquery. form are used together to implement AJAX submission after verification form, jqueryvalidateajax

The basic code is actually very simple, and then it will be expanded at 1.1 points. The final code is written at the end.

Form:

<Form action = "@ Url. action ("AddColumns", "Content ") "method =" post "id =" AddColumnsForm "> <div class =" form-group js-EditCol "id =" AddName "> <label class =" control-label "> name </label> <input Name = "name" class = "form-control" required/> </div> <div class = "form-group js-EditCol" id = "AddRemark"> <label class = "control-label"> remarks </label> <input name = "Remark" class = "form-control"/> </div> <div class = "form-group js-EditCol" id = "AddColumnTypeId"> <label class = "control-label"> type </label> <select class = "form-control "name =" ColumnTypeId "id =" AddColumnTypeIdSel "required> </select> // if the Value of the project is null for empty verification in the drop-down list, it is considered null. </div> <input type = "submit" class = "btn-primary" value = "add topic"/> <input type = "reset" class = "btn-default" value = "clear"/> </form>

Javascript:

$(document).ready(function () {  $("#AddColumnsForm").validate({    submitHandler: function(form) {      $(form).ajaxSubmit();    }  });});

 

Later, I used easyui to refresh the data displayed on the page.

$ (Document ). ready (function () {$ ("# AddColumnsForm "). validate ({submitHandler: function (form) {$ (form ). ajaxSubmit ({success: function (result) {// update the data displayed on the page after the form is submitted $ ('# treetable '). treegrid ('reload'); var d = result. split (';'); ShowMsg (d [0], d [1], d [2]) ;}}) ;});

 

Then, modify the display location of the error message to conform to the bootstrap style.

$ (Document ). ready (function () {$ ("# AddColumnsForm "). validate ({errorPlacement: function (error, element) {element. next ('span. help-Block '). remove (); element. after ('<span class = "help-block">' + error. text () + '</span>');}, success: function (label) {// Add it. If this code is not added, the error message is not canceled after verification }, submitHandler: function (form) {$ (form ). ajaxSubmit ({success: function (result) {$ ('# TreeTable '). treegrid ('reload'); var d = result. split (';'); ShowMsg (d [0], d [1], d [2]) ;}});

Then add the validation style of bootstrap.

$(document).ready(function () {  $("#AddColumnsForm").validate({    errorPlacement: function (error, element) {      element.next('span.help-block').remove();      element.after('<span class="help-block">' + error.text() + '</span>');    }, success: function (label) {    }, highlight: function (element, errorClass, validClass) {      $(element).parent().addClass('has-error');    }, unhighlight: function (element, errorClass, validClass) {      $(element).parent().removeClass('has-error');    },  submitHandler: function (form) {      $(form).ajaxSubmit({        success: function (result) {          $('#TreeTable').treegrid('reload');          var d = result.split(';');          ShowMsg(d[0], d[1], d[2]);        }      });    }  });

 

Then there is the error message text.

$ (Document ). ready (function () {$ ("# AddColumnsForm "). validate ({errorPlacement: function (error, element) {element. next ('span. help-Block '). remove (); element. after ('<span class = "help-block">' + error. text () + '</span>') ;}, success: function (label) {}, highlight: function (element, errorClass, validClass) {$ (element ). parent (). addClass ('has-error');}, unhighlight: function (element, errorClass, ValidClass) {$ (element). parent (). removeClass ('has-error') ;}, messages: {Name: "The column Name is required. ", ColumnTypeId:" select the column type. ", UpColumnId:" columns at the upper level must be selected "}, submitHandler: function (form) {$ (form ). ajaxSubmit ({success: function (result) {$ ('# TreeTable '). treegrid ('reload'); var d = result. split (';'); ShowMsg (d [0], d [1], d [2]) ;}});

 

In the end

$ (Document ). ready (function () {$ ("# AddColumnsForm "). validate ({errorPlacement: function (error, element) {// an error message is displayed after an error occurs. The bootstrap auxiliary text error is the lable that displays the error information by default, and the element is the error text box element. next ('span. help-Block '). remove (); element. after ('<span class = "help-block">' + error. text () + '</span>');}, success: function (label) {// although there is no operation in it, however, if this line of code is not added, the text will not disappear after the verification is successful. }, Highlight: function (element, errorClass, validClass) {// adds the bootstrap verification failure style after the verification fails. $ (Element ). parent (). addClass ('has-error');}, unhighlight: function (element, errorClass, validClass) {// remove the bootstrap verification failure style after verification fails. $ (Element). parent (). removeClass ('has-error') ;}, messages: {// custom error message text Name: "A column Name is required. ", ColumnTypeId:" select the column type. ", UpColumnId:" columns at the upper level must be selected "}, submitHandler: function (form) {$ (form ). ajaxSubmit ({// code success: function (result) {$ ('# treetable') in the pop-up prompt box after the form is submitted successfully '). treegrid ('reload'); var d = result. split (';'); ShowMsg (d [0], d [1], d [2]) ;}});

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.