Common examples of Jquery

Source: Internet
Author: User

Common examples of Jquery

After searching for the Internet for a long time, I did not find it. I verified whether an input id = "yes" in the single-choice radio was selected, so I wrote one myself:

Note: 1. obj is a JQuery object, $ (this) calls thisYou can use the validateIschecked method and then input obj: $ ("# yes ")

// Radio ischecked
$. Fn. validateIschecked = function (obj ){
Var flag = obj. attr ('checked ');
/* Alert (flag );*/
If (flag = "checked "){
/* Alert ("true ");*/
Return true;
} Else {
/* Alert ("false ");*/
Return false;
}
};


The following example shows how to verify non-null, length, and regular expressions written by myself:

// Is empty
$. Fn. validateEmpty = function (obj ){
If (obj. val (). trim () = ""){
Return false;
} Else {
Return true;
}
};
// Length Verification
$. Fn. validateLength = function (obj, n, m ){
Var l = obj. val (). trim (). length;
If (l <= m & l> = n ){
Return true;
} Else {
Return false;
}
};


// Common validate reqular expression
Function validateRegular (obj1, obj2, language, n, m ){

Var reg1 =/^ [\ u4e00-\ u9fa5A-Za-z0-9 \ s {2,}. ()] {} $ /;
Var reg2 =/^ [A-Za-z0-9 \ s {2,}. ()] {} $ /;
If ($ (obj1). validateEmpty ($ (obj1 ))){
If ($ (obj1). validateLength ($ (obj1), n, m )){
If (language = "cn "){
/* Alert ("sss ");*/
If ($ (obj1). val (). match (reg1 )){
/* Alert ("sss ");*/
Optional (obj2).html ("");
} Else {
(Obj2).html ("can only be chinese a-z A-Z 0-9. () space! ");
}
} Else if (language = "en "){
If ($ (obj1). val (). match (reg2 )){
/* Alert ("sss ");*/
Optional (obj2).html ("");
} Else {
(Obj2).html ("can only be a-z A-Z 0-9. () space! ");
}
} Else {
Alert ("js input error! ");
}
} Else {
Authorization (obj2).html (n + "-" + m + "length! ");
}
} Else {
Optional (obj2).html ("");
}


};


Common Jquery Verification:

// Verify the license plate number
$. Fn. validatePlateNo = function (obj ){
Var reg =/^ [\ u4e00-\ u9fa5] {1} [a-zA-Z] {1} [0-9a-zA-Z] {5} $ /;
If (obj. val (). trim (). length = 0 |! Obj. val (). match (reg )){
Return false;
} Else {
Return true;
}
};
// Verify the ID card number
$. Fn. validateIdcardno = function (obj ){
If (checkIDCard (obj. val ())){
Return true;
} Else {
Return false;
}
};
// Only man
$. Fn. validateIdeograph = function (obj ){
Var reg =/^ [\ u4e00-\ u9fa5] {2, 5} $ /;
If (obj. val (). trim (). length = 0 |! Obj. val (). match (reg )){
Return false;
} Else {
Return true;
}
};
// Invalid character Verification
$. Fn. validateIllegalMark = function (obj ){
Var reg =/^ [0-9a-zA-Z] + $ /;
If (obj. val (). match (reg )){
Return true;
} Else {
Return false;
}
};
// Mobile phone number
$. Fn. validateMobile = function (obj ){
Var num = obj. val ();
Var reg =/^ (\ d {3} \) | (\ d {3 }\-))? 13 \ d {9} | 14 [57] \ d {8} | 15 \ d {9} | 18 \ d {9} $ /;
If (num. length! = 11 |! Num. match (reg )){
Return false;
} Else {
Return true;
}
};
// Phone number
$. Fn. validateTelephone = function (obj ){
Var reg =/^ (0 \ d {2, 3})-) (\ d {7, 8}) $ /;
If (obj. val (). length = 0 | obj. val (). match (reg )){
Return true;
} Else {
Return false;
}
};
// QQ
$. Fn. validateQQ = function (obj ){
Var reg =/^ [0-9] {2, 15} $ /;
If (obj. val (). length = 0 | obj. val (). match (reg )){
Return true;
} Else {
Return false;
}
};
// Email
$. Fn. validateMail = function (obj ){
Var reg =/^ \ w + (-\ w +) | (\. \ w +) * \ @ [A-Za-z0-9] + ((\. |-) [A-Za-z0-9] + )*\. [A-Za-z0-9] + $ /;
If (obj. val (). length = 0 | obj. val (). match (reg )){
Return true;
} Else {
Return false;
}
};
// Does not contain special characters
$. Fn. validateVenderName = function (obj ){
Var reg =/^ [\ u4e00-\ u9fa5A-Za-z0-9] {} $/gi;
If (obj. val (). match (reg )){
Return true;
} Else {
Return false;
}
};


Obtain the selected value
Obtains the value of a set of radio selected items.
Var item = $ ('input [@ name = items] [@ checked] '). val ();

Obtain the text of the selected select item.
Var item = $ ("select [@ name = items] option [@ selected]"). text ();

The second element in the select drop-down box is the selected value.
$ ('# Select_id') [0]. selectedIndex = 1;

The second element of the radio Group is the currently selected value.
$ ('Input [@ name = items] '). get (1). checked = true;


$ ("Input [@ type = radio] [@ checked]"). val ();


Get value:
Text Box, text area:
$ ("# Txt"). attr ("value ");

$ ("# Txt"). val ();


Multiple selection box checkbox:
$ ("# Checkbox_id"). attr ("value ");

Drop-down box select:
$ ('# Sel'). val ();

Control form elements:
Text Box, text area:
$ ("# Txt"). attr ("value", ''); // clear the content
$ ("# Txt"). attr ("value", '11'); // fill in the content

Multiple selection box checkbox:
$ ("# Chk1"). attr ("checked", ''); // do not check
$ ("# Chk2"). attr ("checked", true); // check
If ($ ("# chk1"). attr ('checked') = undefined) // checks whether checked undefined has been checked. This occurs after JQ1.6.

Radio Group radio:
$ ("Input [@ type = radio]"). attr ("checked", '2'); // set the project with value = 2 to the selected one

Drop-down box select:
$ ("# Sel"). attr ("value", '-sel3'); // set the value =-sel3 project to the currently selected item
$ (" 1111 2222"). AppendTo (" # sel ") // Add the option in the drop-down box
$ ("# Sel"). empty (); // clear the drop-down list

==================================

In Jquery, $ ("# id") is used to obtain the page input element, which is equivalent to document. getElementById ("element"). However, this is a Jquery object instead of a dom element Object. value is the attribute of the dom element Object. therefore, use $ ("# id "). value cannot be obtained;

The procedure is as follows:

Valid value:

Val = $ ("# id") [0]. value;

$ ("# Id") [0]. value = "new value ";

Assignment:

$ ("# Id") [0]. value = "new value ";
Or $ ("# id"). val ("new value ");

Val = $ ("# id"). attr ("value ");

Jquery input text radio check select Operation

[Xhtml]View plaincopy
  1. Untitled document
  2. Dd
  3. Ff
  4. 55
  5. Jgdg
  6. Jgdg
  7. D G


  8. Fgoohello
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.