The basic method of value and assignment of JQuery

Source: Internet
Author: User

  This article mainly introduces the basic methods of jquery value and assignment, the need for friends can refer to the following

/* Get Text.areatext value */  var textval = $ ("#text_id"). attr ("value"); //  var textval = $ ("#text_id"). val (); /* Gets the value of the radio button */  var valradio = $ ("input[@type =radio][@checked]"). Val (); /* Get a group of names (items) Radio the value of the selected item */  var item = $ (' input[@name =items][@checked] '). Val (); /* Get check box value */  var checkboxval = $ ("# checkbox_id "). attr (" value "); /* Gets the value of the Drop-down list */  var selectval = $ (' #select_id '). Val ();   //text box, text area:   $ ("#text_id"). attr ("Value", "");/empty content   $ ("#text_id"). attr ("value", ' test ');/fill content  //Multiple-selection box checkbox:   $ ("#chk_id"). attr ("Checked", "");//Make it not checked   $ ("#chk_id"). attr ("Checked", true);//tick   if ($ ("#chk_id") . attr (' checked ') ==true)//Determine if    //Radio Group radio:    $ ("input[@type =radio]" is selected. attr ("Checked", ' 2 ') //Set value=2 item is the current check    //dropdown box select:  $ ("#select_id"). attr ("value", ' test ');/set value= The project for test is currently selected   $ ("<option value= ' test" >test</option><option value= ' test2 ' >test2</option> "). Appendto (" #select_id ")//Add option  $ (" #select_id ") of the Drop-down box. Empty ()/clear dropdown     Gets the value of a set of radio items named (items)   var item = $ (' input[@name =items][@checked] '). val () = undefined  if not selected Gets the text of the Select selected item   var item = $ ("select[@name =items] option[@selected]"). Text ();  The second element of the Select Drop-down box is the current selected value   $ (' #select_id ') [0].selectedindex = 1;  Radio The second element of a single selection group is the currently selected value   $ (' input[@name =items] '). Get (1). Checked = true;   //Reset form   $ ("form"). each (function () { . Reset (); });    1. The selection element   $ ("#myid") Effect equals document.getElementById ("myID"), but the characters are much less written .    If you need to convert a jquery object to an HTML element, You just need to take the No. 0 element. For example, $ ("#myid") returns a JQuery object, and $ ("#myid") [0] returns HTML elements     If you select all of the IMG elements, then write: $ ("img")     If you select a DIV element with a class= "TextBox" (<div class= "textbox" ></div>), then write: $ ("div. TextBox ")     SELECT element with MyAttr attribute $ (" div[myattr] ")   Select the element $ (" div[myattr= ") with the MyAttr attribute and the property value equal to MyClass MyClass '] " property is not equal to [myattr!= ' MyClass ']  property with my start [myattr^= ' my ']  attribute ends with class [myattr$= ' class ']  property contains cla three characters [myattr* = ' CLA ']    If a selection returns multiple elements, and you want to apply some attributes to that element every time you return an element, you can write   $ ("div"). each (function ()   {  $ ( This). CSS ("Background-color", "#F00 ″");  alert ($ (this). html ());  $ (this). Width ("200px"); });    2. Event   to page plus onload event handling   $ (function ()   {  alert ("page structure loaded, but some pictures may not have been loaded (in general, this event is enough)"); nbsp ;    can bind multiple onload event handling methods   $ (function ()   {  alert ("I was executed first"); });    $ ( function ()   {  alert ("I am second to execute"); });    BIND special events   $ ("#myid"). KeyDown (function ()   {   Alert ("Triggers the KeyDown event"); });    In addition to these common, infrequently used events need to bind     3 through the Bind method. Element Properties/Methods   Gets the height of an element, $ ("#myid"). Height ()   Gets the position of an element, $ ("#myid"). Offset () Returns an offset object, and if the top of the element position is taken, $ ("# myID "). Offset (). Top,? Take left $ (" #myid "). Offset () .left  gets the innerHTML of an element, $ (" #myid "). html()   Get an element of innertext, $ ("#myid"). Text ()   Get the value of a text box, $ ("#myid"). Val ()   Gets the attributes of an element, $ ("#myid"). attr (" MyAttribute ")     above these methods have the basic feature of taking values without parameters, with parameters representing the set value (except for offset), such as   $ (" #myid "). Height (" 20″);  $ (" #myid "). HTML (" <a href= >asdasd</a> ")   $ (" #myid "). Val (" ASDASD ")     Note that offset is read-only.     Set attribute $ ("#myid") to an element. attr ("width", "20%")   read a property $ ("#myid"). attr ("width")   Specify multiple attributes at once $ ("#myid" ). attr ({disabled: "Disabled", Width: "20%", Height: "30″}")   Delete property $ ("#myid"). Removeattr ("disabled")     Apply Style $ ("#myid"). AddClass ("MyClass")   Delete style $ ("#myid"). Removeclass ("MyClass")     plus a style $ ("#myid"). CSS (" Height "," 20px ")   plus a group of styles $ (" #myid "). CSS ({height:" 20px ", Width:" 100px "})   NOTE: If you add a style, the name of the style is the name in the CSS, For example style= "Background-color: #FF0000 ″, the corresponding jquery notation is $ (" #myid "). CSS (" Background-color "," #FF0000 ″ ")   But when you add a set of styles, The name of the style is the CSS name in JavaScript, for example: Myid.style.backgroundColor = "#FF0000 ″, and the corresponding jquery notation is $ ("#myid"). CSS ({backgroundcolor: "#FF0000 ″})     4. Find elements based on relationships   find the next element of the same sibling $ ("#myid"). Next ()   Find all of the elements below yourself $ ("#myid"). Nextall ()   Find and own the same sibling of the previous element $ ("# myID "). Prev ()   Find all elements above themselves $ (" #myid ") for each sibling. Prevall ()   find its own first generation child element $ (" #myid "). Children ()   Find your own first parent element $ ("#myid"). Parent ()   Find all of their parent elements $ ("#myid"). Parents ()   Example:  $ ("Div.l4″"). Parents (). each (   function () {  alert ($ (this). html ()); });    will get all the parent elements of the Class=l4 div and alert out their HTML     Example:  $ ("Div.l4″"). Parents ("Div.l2″"). each (function () {Alert ($ (this). html ());   Gets the parent element of the CLASS=L4, the parent element must be a div, and its class=l2    all of the methods mentioned here can be represented with expressions that refer to the first part     5. Maintain element   Add an element   $ ("body") to the body. Append ("<input type= ' text ' value= ' asd '/>")   The statement inserts this HTML into the body end tag, resulting in <input type= ' text ' value= ' ASD '/></body>    $ ("body"). Prepend (" <input type= ' text ' value= ' asd '/> ')   The statement inserts this HTML into the body start tag, and the result is <BODY><input type= ' text ' value= ' ASD '/>    6.ajax  request a page   $.get ("http://www.google.com") using the Get method, "Q=jquery", function (data, status) {alert (data)}   = Request http://www.google.com, parameter is Q, the value of the parameter is jquery, after the request (whether successful or failed) Performs the following function, which has two fixed parameters, data and status, which is the returned, status is the state of this request     requests a page with the Post method   $.post (...) Parameter with Get method     7. Other methods   $.trim (STR) will take the Str before and after the space to remove   $.browser to return the current user browser type   $. Browser.version returns the current browser version     8. Plug-ins   jquery Support Plug-ins, http://jquery.com/plugins/has a lot of ready-made plug-ins, you can write   write their own plug-ins, please refer to Http://docs.jquery.com/Core/jQ ... end#object and http://docs.jquery.com/core/jquery.extend#object    1, dropdown box:  var cc1 = $ (". FORMC Select [@name = ' country '] option[@selected] "). Text (); Gets the text of the selected item of the Drop-down menu (note that there are spaces in the middle)   var CC2 = $ ('. FORMC select[@name = ' country '] '). Val (); Gets the value of the selected item for the Drop-down menu   var cc3 = $ ('. FORMC select[@name = ' country '] '). attr ("id"); Gets the id attribute value   $ ("#select") for the selected item in the Drop-down menu. Empty ()//Empty Drop-down box//$ ("#select"). htmL (');  $ ("<option value= ' 1 ' >1111</option>"). Appendto ("#select")//Add a Drop-down box to the option  a little explanation:   1.select[@name = ' country '] option[@selected] Represents a SELECT element with the name attribute,  and the property value ' country ' with selected The option element;  of a property can be seen as having a @ at the beginning of a property that follows.     2, Radio box:  $ ("input[@type =radio][@checked]"). Val (); Gets the value of the selection of the radio box (note that there are no spaces in the middle)   $ ("input[@type =radio][@value =2]"). attr ("Checked", ' checked '); Sets the value=2 of the radio box to the selected state. (Note that there are no spaces in between)     3, check box:  $ ("input[@type =checkbox][@checked]"). Val (); The value of the first item selected for the check box   $ ("input[@type =checkbox][@checked]"). each (function () {//The check box is typically selected for more than one, so you can loop out   alert ($ (this). Val ()); });    $ ("#chk1"). attr ("Checked", "");//Do not tick   $ ("#chk2"). attr ("Checked", true) ;//tick   if ($ ("#chk1"). attr (' checked ') ==undefined) {}//judge whether it has been ticked  

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.