Through JS, modify all form forms and submit data in JSON format.

Source: Internet
Author: User

Directly on the code

<script>
$ (function () {///Get all form forms in the Web page $ ("form"). each (function () {////Registration form's Commit event $ (this). Submit (Function (event) {//Blocked form registration Event.preventdefault (); Gets the URL var url = $ (this). attr ("action"); Request (URL, ' POST ', json.stringify ($ (this). SerializeObject ()), function () {alert (' 123 ');}, null);} );});}); Convert the $.ajax function to a simple interface functions request (URL, method, param, callback, fail) {//alert (param); $.ajax ({type:method,//async:false, ContentType: "Application/json;charset=utf-8", DataType: "JSON", Url:url, Data:param, Success:callback, Error:fail,}); }//convert the data inside the form form to a JSON object $.fn.serializeobject = function () {var o = {}; var a = This.serializearray (); $.each (A, function () {if (O[this.name]!== undefined) {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; };//then converts the JSON object into a JSON string using the Json.stringify () function.
</script>

 

Refer to the blog for a few changes.

Through JS, modify all form forms and submit data in JSON format.

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.