JQuery extension method implements instance code with JSON mutual conversion

jQuery notes

, using jQuery implementation JSON and rear end interaction from the form, and the JSON returned by the rear is mapped to the form corresponding to the form.

Converts the form of JSON object
   
// convert the form to the JSON object $ .fn.tojson = function () {var self = this, json = {}, push_counters = {}, patterns = {“validate”: / ^ [A-ZA-Z] [A-ZA-Z0-9 _] * (?: \ [( ?: \ D * | [A-ZA-Z0-9 _] +) \]) * $ /, “key”: / [a-za-z0-9 _] + | (? = \ [\]) / g, ” Push “: / ^ $ /,” fixed “: / ^ \ d + $ /,” named “: / ^ [A-ZA-Z0-9 _] + $ /}; this.build = function (base, key, value ) {base [key] = value; return base;}; this.push_counter = function (key) {if (push_counters [key] === undefined) {push_counters [key] = 0;} return push_counters [key] ++ }; $ .each ($ (this) .SerializeArray (), FunctiON () {// Skip Invalid Keys if (! patterns.validate.test (this.name)) {return;} var k, keys = this.name.match (patterns.key), merge = this.Value, Reverse_Key = this.name; while (k = keys.pop ())! == undefined) {// adjust reverse_key reverse_key = reverse_key.replace (new regexp (“\\ [” + k + “\\] $”) , ”); // push if (k.match (patterns.push)) {merge = self.build ([], self.push_counter (reverse_key), merge);} // fixed else if (k.match Patterns.fixed) {merge = self.build ([], k, merge);} // named else if (k.match (patterns.named)) {merge = self.build ({}, k, merge) }} JSON = $ .Extend (True, JSON, MERGE);}); return json;};

assigns a JOSN object to FORM to make the form control also display the corresponding state
  < arr.length; i++) {
                if ($(this).val() == arr[i]) {
                    $(this).attr('checked', true);
                  break;
                }
              }
            } else {
              $(this).val(value);
            }
          } else if (tagName == 'SELECT' || tagName == 'TEXTAREA') {
            $(this).val(value);
          }
        });
      }
    }
  } // assigns the JOSN object to Form $ .fn.loadData = function (obj) {var Key, Value, Tagname, Type, Arr; this.reset (); for (var x obj) {IF (Obj .hasownproperty (x)) {key = x; value = obj [x]; this.find ("[Name = '" + key + "], [name ='" + key + "[] ']") ("function () {tagname = $ (this) [0] .tagname.touppercase (); type = $ (this) .attr ('type'); if (tagname == 'input') {i (Type == 'Radio') {IF ($ (this) .val () == value) {$ (this) .attr ('Checked', true);}} else if (type == 'checkbox') {Arr = Value.split (','); for (var i = 0; i 

Supplement: Two extensions in jQuery

in jQuery, there are two extensions

1. Class method ($ .Extend ())

$ .Extend ({Print1: function (name) {// print1 is a function defined Name, Name in parentheses is the parameter console.log (name)}}); $ .print1 (“Kun”); // Direct $. Function name (parameter);
    2. Object Method ($ .fn.extend ())  

$ .fn.extend ({GetMax: Function (a, b) {var result = a> b? a: b; console.log (result);}}); $ (“infut”). getmax (1, 2); // call Time to $ (label name). Function name ();
   3. In general, jQuery's extended method writes in self-execution anonymous functions (CAUSE) In JS, it is a function domain. Writing in the function can avoid its own defined function or variables and external conflicts)      
( Function () {$ .extent ({print1: function () {c”}}}} ();

Summary

Xiaobi introduced the jQuery extension method to implement the instance code of the Form form and JSON, I hope to help everyone. If you have any questions, please leave a message, the small package will reply to everyone. Thank you very much for your support of Tumi Cloud Website!
                     
© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment