jquery Plugin: Ajax automatically binds JSON data to form forms

Source: Internet
Author: User

Two common ways to register a jquery method:

// jquery static method registration // Call Method $.a1 () $.extend ({    function  () {        Console.log ("A1");    }}) // jquery Plugin Method Registration // Call the method $ ("#col"). B1 () $.fn.extend ({    function  () {        Console.log ("B1");    }})

The data returned by Ajax is automatically bound to the plug-in in form form, the usual language modification and other business, the source code is as follows:

/*flagname: An attribute that identifies a bound field element, such as Data-bind is a property that looks for a bound field: <input data-bind= ' time ' type= ' text '/>jsondata:json data object, Based on the property name, the field that is bound to the corresponding property name*/varJsonbind =function(Ele, opt) { This. $element =$ (ele), This. defaults ={flagname:' Name '    },    /*When you pass more than one argument to the Extend method, it merges all the parameter objects into the first one.    At the same time, if the object has the same name attribute, then the merge will overwrite the previous one.    The advantage of having a new empty object as the first parameter of $.extend, followed by defaults and user-passed parameter objects, is that all values are merged into the empty object, protecting the default values inside the plug-in. */     This. Options = $.extend ({}, This. defaults, opt)}; Jsonbind.prototype={bind:function () {        if( This. Options.jsondata) {            if( This. Options.jsondata = = ' String ') {                 This. Options.jsondata = Json.parse ( This. Options.jsondata); }        }        //get the element that triggered the event        varDom = This. $element; varName = This. Options.flagname; vardata = This. Options.jsondata; //iterate through all elements within an element that contain the name attribute        //return makes it only a chained call, such as: $ ("#col"). Get (). Set (). Trim ()        returnDom.find ("[" + Name + "]"). each (function () {            //Remove the value of the corresponding Name property in the JSON            varKey = $ ( This). attr (name); varval =$.trim (Data[key]); if(val! = ") {                //checks the current element label and assigns a value to different labels                if($( This). Is ("Input")) {                    //checks the current element type and assigns values to different types                    Switch($( This). attr ("type")) {                         Case"Radio": $ (DOM). Find ("input:radio[" + name + "= '" + key + "'][value= '" + val + "']"). Prop ("Checked",true);  Break;  Case"checkbox": $ (DOM). Find ("input:checkbox[" + name + "= '" + key + "'][value= '" + val + "']"). Prop ("Checked",true); //$ (this). Siblings ("[value= '" + val + "']"). Prop ("checked", true);                             Break; default:                            $( This). Val (Val);  Break; }                }                Else if($( This). Is ("select")) {                    $( This). Val (Val);    }            }        }); }};//adding methods to the jquery extension method$.fn.extend ({dataBind:function(options) {varJsbind =NewJsonbind ( This, Options); returnJsbind.bind (); }});

Call Mode:

// FlagName is an optional parameter, and the default is the element's Name property $ ("#menuForm"). DataBind ({    jsonData:JSON.parse (data),    FlagName: ' name '});

jquery Plugin: Ajax automatically binds JSON data to form forms

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.