Home > front end >  About $. The extend ($fn, {} a few questions, masters, please help to look at.
About $. The extend ($fn, {} a few questions, masters, please help to look at.

Time:11-17

The following code, excuse me:

1, when the validate function will be triggered,

2, $. The extend ($. The fn. What is the extend usage, $. Fn is what things?

(function ($) {

$. The extend ($fn, {
//http://docs.jquery.com/Plugins/Validation/validate

Validate: function (options) {


//if nothing is selected, the return nothing. Can 't chain anyway
if ( ! This. Length) {
If (options & amp; & The options. The debug & amp; & Window. The console) {
The console. Warn (" Nothing selected, can 't validate, returning Nothing. ");
}
return;
}

//check if a validator for this form was already created
Var validator=$. Data (this [0], "the validator");
If (validator) {
Return the validator.
}

//Add novalidate tag if it.
Enclosing attr (" novalidate novalidate ", "");

The validator=new $. The validator (options, this [0]).
$. Data (this [0], "the validator", the validator);

If (validator. Settings. Onsubmit) {

Enclosing validateDelegate (" : submit ", "click", function (event) {
If (validator. Settings. SubmitHandler) {
The validator. SubmitButton=event. The target;
}
//allow suppressing the validation by adding a cancel the class to the submit button
If ($(event. The target). HasClass (" cancel ")) {
The validator. CancelSubmit=true;
}

//allow suppressing the validation by adding the HTML 5 formnovalidate attribute to the submit button
If ($(event. The target). Attr (" formnovalidate ")!==undefined) {
The validator. CancelSubmit=true;
}
});

//validate the form on the submit
This. Submit (function (event) {
If (validator. Settings. The debug) {
//prevent form submit to be able to see the console output
event.preventDefault();
}
The function handle () {
Var hidden;
If (validator. Settings. SubmitHandler) {
If (validator. SubmitButton) {
//insert a hidden input as a replacement for the missing submit button
Hidden=$(" & lt; Input type='hidden'/& gt;" ). Attr (" name ", the validator. SubmitButton. Name). The val ($(validator. SubmitButton). Val ()). The appendTo (validator. CurrentForm);
}
The validator. Settings. SubmitHandler. Call (the validator, the validator currentForm, event);
If (validator. SubmitButton) {
//and clean up afterwards. Thanks to the no - block - scope, hidden can be referenced
Hidden. Remove ();
}
return false;
}
return true;
}

//prevent the submit for invalid forms or custom submit handlers
If (validator. CancelSubmit) {
The validator. CancelSubmit=false;
Return the handle ();
}
{if (validator. The form ())
If (validator. PendingRequest) {
The validator. FormSubmitted=true;
return false;
}
Return the handle ();
} else {
The validator. FocusInvalid ();
return false;
}
});
}

Return the validator.
},
//http://docs.jquery.com/Plugins/Validation/valid
Valid: function () {
If ($(this [0]) is (" form ")) {
Return this. The validate (). The form ();
} else {
Var valid=true;
Var validator=$(this [0]. The form). The validate ();
This. Each (function () {
Valid=valid & amp; & The validator. Element (this);
});
Return valid;
}
},
//the attributes: space seperated list of attributes to retrieve the and remove
RemoveAttrs: function (attributes) {
Var result={},
$element=this;
$. Each (attributes. The split (/\/s), function (value) index, {
The result/value=$element. Attr (value);
$element. RemoveAttr (value);
});
return result;
},
//http://docs.jquery.com/Plugins/Validation/rules
Rules: function (command, argument) {
Var element=this [0];

If (command) {
Var Settings=$. Data (element. The form, "the validator"). The Settings;
Var staticRules=Settings. The rules;
Var existingRules=$. The validator. StaticRules (element);
The switch (command) {
A case of "add" :
$. The extend (existingRules, $. The validator. NormalizeRule (argument));
//remove the messages from the rules, but allow them to be set separetely
The delete existingRules. Messages;
StaticRules [element name]=existingRules;
If (argument. Messages) {
Settings. The messages [element name]=$. The extend (Settings) messages [element. The name], argument. The messages).
}
break;
Case "remove" :
if ( ! Argument) {
The delete staticRules [element name];
Return existingRules;
}
Var filtered={};
$. Each (argument. The split (/\/s), function (index, method) {
Filtered. [method]=existingRules. [method];
The delete existingRules. [method];
});
Return the filtered;
}
}

Var data=https://bbs.csdn.net/topics/$.validator.normalizeRules (
$. The extend (
{},
$. The validator. ClassRules (element),
$. The validator. AttributeRules (element),
$. The validator. DataRules (element),
$. The validator. StaticRules (element)
), element);

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull