Common methods of jquery

Source: Internet
Author: User

0. Common code: (1) Ajax request
$ (function () {$ (' #send '). Click (function () {$.ajax ({type: "GET",//get or Post,async:true,//default is set to True, all requests are asynchronous requests.) URL: "http://www.idaima.com/xxxxx.php", Data: {username: $ ("#username"). Val (), Content: $ ("#content"). Val ()}, DataType: "JSON",//xml, HTML, script, JSONP, Textbeforesend:function () {},complete:function () {},success:function ( Data) {alert (data)},error:function () {},});});

  

(2) How to obtain a checkbox and decide whether to select
$ ("input[type= ' checkbox ']"). Is (': Checked '// return result: Check =true, unchecked =false
(3) Get the checkbox selected value
var chk_value =[]; $ (' input[name= ' test ']:checked '). each (function() {     chk_ Value.push ($ (this). Val ()); });
(4) CheckBox Select all/reverse selection/odd
$ ("document"). Ready (function() {    $("#btn1"). Click (function() {        $("[Name= ' checkbox ']"). attr ("Checked", ' true ');//Select All}) $ ("#btn2"). Click (function() {        $("[Name= ' checkbox ']"). Removeattr ("checked");//Cancel Select all}) $ ("#btn3"). Click (function() {        $([name= ' checkbox ']:even '). attr ("Checked", ' true ');//Check all odd}) $ ("#btn4"). Click (function() {        $("[Name= ' checkbox ']"). each (function() {//Inverse Selection            if($( This). attr ("Checked")) {                $( This). Removeattr ("Checked"); } Else {                $( This). attr ("Checked", ' true '); }        })    })})
(5) Get the value of the Select drop-down box

In fact, the simplest drop-down box, direct Val on the line

$ ("#select"). Val ()
(6) To get the selected value, three ways can:
$ (' input:radio:checked '). Val (); $ ("input[type= ' Radio ']:checked"). Val (); $ ("input[name= ' Rd '): Checked "). Val ();

(7) Set the first radio as the selected value, both methods can:

$ (' Input:radio:first '). attr (' checked ', ' checked '); $ (' Input:radio:first '). attr (' checked ', ' true ');
(8) Set the last radio as the selected value:
$ (' Input:radio:last '). attr (' checked ', ' checked '); $ (' Input:radio:last '). attr (' checked ', ' true ');
(9) Set any one of the radio to the selected value according to the index value:
$ (' Input:radio '). EQ (index value). attr (' checked ', ' true '); // index value =0,1,2 .... $ (' Input:radio '). Slice. attr (' checked ', ' true ');
(10) Set radio to the selected value based on value
$ ("input:radio[value= ' Rd2 ')"). attr (' checked ', ' true '), $ ("input[value= ' Rd2 ']"). attr (' checked ', ' true ');
1. References to page elements

The $ () reference element through jquery includes methods such as the ID, class, element name, and the hierarchy of elements and Dom or XPath conditions, and the returned object is a JQuery object (collection Object) and cannot be called directly by the DOM-defined method.

2. The conversion of JQuery objects to DOM objects

Only jquery objects can use the method defined by jquery. Note that DOM objects are different from jquery objects, and you should be aware of whether you are manipulating DOM objects or jquery objects when calling methods.

Ordinary DOM objects can generally be converted to jquery objects by $ ().

Such as:

$ (document.getElementById ("MSG"))

is a jquery object, you can use the jquery method.

Because the jquery object itself is a collection. So if a jquery object is to be converted to a DOM object, it must take one of these items, which is generally available through the index.

Such as:

$ ("#msg") [0],$ ("div"). EQ (1) [0],$ ("div"). get () [1],$ ("TD") [5]

These are DOM objects, and you can use the methods in the DOM, but you can't use jquery any more.

The following are the correct ways to do this:

$ ("#msg"). html (); $ ("#msg") [0].innerhtml;$ ("#msg"). EQ (0) [0].innerhtml;$ ("#msg"). Get ( 0). InnerHTML;
3. How to get an item of the jquery collection

For a collection of captured elements, getting one of these items (specified by index) can be obtained using the EQ or get (n) method or index number, note that the EQ returns the JQuery object, and the Get (n) and the index return the DOM element object. jquery can only be used for jquery objects, whereas DOM objects can only use the DOM method, such as to get the contents of a third <div> element. There are two ways to do this:

// methods for invoking jquery objects // invoking the Dom's method properties

Common methods of jquery

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.