JQuery learning notes --- get the form value (Input Checkbox ListBox Button ...)

Source: Internet
Author: User

 

1. Question about jquery getting input value

$ ("") Is a jquery object, not a dom element

Value is the attribute of dom element.

Jquery corresponds to val.

Val (): obtains the current value of the First Matching Element.

Val (val): sets the value of each matching element.

Therefore, the code should be written as follows:

Value: val = $ ("# id") [0]. value;

Assignment:

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

Or $ ("# id"). val ("new value ");

 

Alternatively, you can: val = $ ("# id"). attr ("value ");

 

 

1.

2.

3. <title> jquery simple example yanleigis E-mail: landgis@126.com </title>

4. <script type = "text/javascript" src = "jquery. js"> </script>

5. <script language = "JavaScript">

6. $ (document). ready (function (){

7.

8. $ ("# cylok"). click (function () {// only id can be used, add # yanleigis Email: landgis@126.com

9. alert ("Hello world cylok! ");

10 .});

11. $ ("# ylok"). click (function (){

12. alert ("Hello world ylok! ");

13 .});

14. $ ("a"). click (function (){

15. alert ("Hello world! ");

16 .});

17.

18. $ ("# add6"). click (function (){

19. alert ("Hello world add! ");

20 .});

21.

22. $ ("# yltxt"). bind ("change", function (){

23.

24. $ ("# yltxt") [0]. value = "yanlegis ok1"; // method 1

25. alert ("method two get value:" + $ ("# yltxt"). attr ("value "));

26. $ ("# yltxt"). val ("yanlegis ok2"); // method 2

27. alert ("method two get value:" + $ ("# yltxt") [0]. value );

28. // $ ("# yltxt"). attr ("value") = "yanlegis ok3"; // method 3

29.

30.

31 .});

32.

33.

34 .});

35. </SCRIPT>

36.

37. <body>

38.

39. <p>

40. <input type = "button" id = "ylok" value = "yanleigis">

41. <input type = "checkbox" id = "cylok" value = "landgis@126.com">

42. <button id = "add6"> landgis@126.com </button>

43.

44.

45. <a href = ""> Link </a> </p>

46. <p>

47. <input type = "text" id = "yltxt">

48. </p>

49. </body>

50.

 

2. Get the checkbox Value

... Continue

Jquery radio, checkbox, select, radio, checkbox, select, and related

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;

 

Get value:

 

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

Multiple choice box checkbox: $ ("# checkbox_id"). attr ("value ");

Radio Group radio: $ ("input [@ type = radio] [@ checked]"). val ();

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 choice box checkbox: $ ("# chk1"). attr ("checked", ''); // do not check

$ ("# Chk2"). attr ("checked", true); // check

If ($ ("# chk1"). attr ('checked') = undefined) // you can check whether the checked has been checked.

 

Radio Group radio: $ ("input [@ type = radio]"). attr ("checked", '2'); // set the project with value = 2 as the currently selected item

Drop-down box select: $ ("# sel"). attr ("value", '-sel3'); // set the value =-sel3 project to the currently selected item

$ ("<Option value = '1'> 1111 </option> <option value = '2'> 2222 </option> "). appendTo ("# sel") // Add the option in the drop-down box

$ ("# Sel"). empty (); // clear the drop-down list

Bytes ----------------------------------------------------------------------------------------------------

// Traverse option and add or remove option

Function changeShipMethod (shipping ){

Var len = $ ("select [@ name = ISHIPTYPE] option"). length

If (shipping. value! = "CA "){

$ ("Select [@ name = ISHIPTYPE] option"). each (function (){

If ($ (this). val () = 111 ){

$ (This). remove ();

}

});

} Else {

$ ("<Option value = '000000'> UPS Ground </option>"). appendTo ($ ("select [@ name = ISHIPTYPE]");

}

}

 

// Obtain the value of the drop-down list

$ (# TestSelect option: selected'). text ();

Or $ ("# testSelect"). find ('option: selected'). text ();

Or $ ("# testSelect"). val ();

//////////////////////////////////////// //////////////////////////

If your memory is poor, you can add it to your favorites:

1. drop-down box:

Var C0 = $ (". formc select [@ name = 'country'] option [@ selected] "). text (); // get the text of the selected item from the drop-down menu (note that there is a space in the middle)

Var cc2 = $ ('. formc select [@ name = "country"]'). val (); // obtain the value of the selected item from the drop-down menu.

Var cc3 = $ ('. formc select [@ name = "country"]'). attr ("id"); // obtain the ID attribute value of the selected item from the drop-down menu

$ ("# Select"). empty (); // clear the drop-down box // $ ("# select" example .html ('');

$ ("<Option value = '1'> 1111 </option>"). appendTo ("# select") // Add the option in the drop-down box

A little explanation:

1. select [@ name = 'country'] option [@ selected] indicates that the name attribute exists,

In addition, the select element with the selected attribute in the 'country' select element has the selected attribute;

It can be seen that the attribute is followed by the @ parameter.

2, single region:

$ ("Input [@ type = radio] [@ checked]"). val (); // obtain the value of the selected item of a single sequence (note that there is no space in the middle)

$ ("Input [@ type = radio] [@ value = 2]"). attr ("checked", 'checked'); // set single checked value = 2 to the selected status. (Note that there is no space in the middle)

3. Check box:

$ ("Input [@ type = checkbox] [@ checked]"). val (); // obtain the value of the first check box.

$ ("Input [@ type = checkbox] [@ checked]"). each (function () {// Since multiple check boxes are selected, You can output them cyclically.

Alert ($ (this). val ());

});

$ ("# Chk1"). attr ("checked", ''); // do not check

$ ("# Chk2"). attr ("checked", true); // check

If ($ ("# chk1"). attr ('checked') = undefined) {}// you can check whether a check has been performed.

 

Of course, jquery's selector is powerful. There are many other methods.

<Script src = "jquery-1.2.1.js" type = "text/javascript"> </script>

<Script language = "javascript" type = "text/javascript">

$ (Document). ready (function (){

$ ("# SelectTest"). change (function ()

{

// Alert ("Hello ");

// Alert ($ ("# selectTest"). attr ("name "));

// $ ("A"). attr ("href", "xx.html ");

// Window. location. href = "xx.html ";

// Alert ($ ("# selectTest"). val ());

Alert ($ ("# selectTest option [@ selected]"). text ());

$ ("# SelectTest"). attr ("value", "2 ");

});

});

</Script>

 

<A href = "#"> aaass </a>

<! -- Drop-down box -->

<Select id = "selectTest" name = "selectTest">

<Option value = "1"> 11 </option>

<Option value = "2"> 22 </option>

<Option value = "3"> 33 </option>

<Option value = "4"> 44 </option>

<Option value = "5"> 55 </option>

<Option value = "6"> 66 </option>

</Select>

Jquery radio value, checkbox value, select value, radio selected, checkbox selected, select selected, and related to obtain 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;

Get value:

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

Multiple choice box checkbox: $ ("# checkbox_id"). attr ("value ");

Radio Group radio: $ ("input [@ type = radio] [@ checked]"). val ();

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 choice box checkbox: $ ("# chk1"). attr ("checked", ''); // do not check

$ ("# Chk2"). attr ("checked", true); // check

If ($ ("# chk1"). attr ('checked') = undefined) // you can check whether the checked has been checked.

Radio Group radio: $ ("input [@ type = radio]"). attr ("checked", '2'); // set the project with value = 2 as the currently selected item

Drop-down box select: $ ("# sel"). attr ("value", '-sel3'); // set the value =-sel3 project to the currently selected item

$ ("<Optionvalue = '1'> 1111 </option> <optionvalue = '2'> 2222 </option> "). appendTo ("# sel") // Add the option in the drop-down box

$ ("# Sel"). empty (); // clear the drop-down list

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;

Get value:

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

Multiple choice box checkbox: $ ("# checkbox_id"). attr ("value ");

Radio Group radio: $ ("input [@ type = radio] [@ checked]"). val ();

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 choice box checkbox: $ ("# chk1"). attr ("checked", ''); // do not check

$ ("# Chk2"). attr ("checked", true); // check

If ($ ("# chk1"). attr ('checked') = undefined) // you can check whether the checked has been checked.

Radio Group radio: $ ("input [@ type = radio]"). attr ("checked", '2'); // set the project with value = 2 as the currently selected item

Drop-down box select: $ ("# sel"). attr ("value", '-sel3'); // set the value =-sel3 project to the currently selected item

$ ("<Option value = '1'> 1111 </option> <option value = '2'> 2222 </option> "). appendTo ("# sel") // Add the option in the drop-down box

$ ("# Sel"). empty (); // clear the drop-down list

 

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

 

1. Appendix: jquery 1.3.2 Value Method for basic form elements

2.

3.

4./* obtain the value of TEXT. AREATEXT */var textval = $ ("# text_id"). attr ("value"); // or

Var textval = $ ("# text_id"). val ();/* obtain the value of a single-choice button */

Var valradio = $ ("input [type = radio]: checked"). val ();/* Get the value of a set of radio selected items named (items */

Var item = $ ('input [name = items]: checked'). val ();/* Get the value of the check box */

Var checkboxval = $ ("# checkbox_id"). attr ("value");/* Get the value of the drop-down list */

Var selectval = $ ('# select_id'). val ();/* text box, text area */

$ ("# Text_id"). attr ("value", "); // clear the content

$ ("# Text_id"). attr ("value", 'test'); // fill in the Content/

* Multiple-choice box checkbox */$ ("# chk_id "). attr ("checked", "); // make it unchecked $ (" # chk_id "). attr ("checked", true );

// Check if ($ ("# chk_id"). attr ('checked') = true) // determine whether a single-choice group is selected/* radio */

$ ("Input [type = radio]"). attr ("checked", '2'); // set the project with value = 2 to the currently selected item/* drop-down box

Select */$ ("# select_id"). attr ("value", 'test'); // set the project with value = test as the selected item

$("Testtest2 "). appendTo ("# select_id") // Add option $ ("# select_id") in the drop-down box "). empty (); // clear the drop-down box/* Get the value of a set of radio selected items named (items) */var item =$ ('input [name = items]: checked '). val (); // if not selected, val () = undefined/* get 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 currently 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 ;/

* Reset the form */$ ("form"). each (function () {. reset ();});


 

 

From Hurry's column

 

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.