jquery Operation Input Value Summary

Source: Internet
Author: User

Gets the selected value
Gets the value of a set of radio selected items
var item = $ (' input[@name =items][@checked] '). Val ();

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 currently selected value

$ (' #select_id ') [0].selectedindex = 1;

Radio the second element of a radio group is the currently selected value
$ (' input[@name =items] '). Get (1). checked = true;

Get Value:

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

$ ("#txt"). Val ();

Multiple marquee 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 Contents
$ ("#txt"). attr ("value", ' 11 ');//fill content

Multiple marquee checkbox:
$ ("#chk1"). attr ("Checked", "");//Not ticked
$ ("#chk2"). attr ("Checked", true);//tick
if ($ ("#chk1"). attr (' checked ') ==undefined)//Determine if a tick has been made

Radio Group Radio:
$ ("input[@type =radio]"). attr ("Checked", ' 2 ');//Set value=2 for the currently selected item

Drop-down box select:
$ ("#sel"). attr ("value", '-sel3 ');//Set the VALUE=-SEL3 item as the current selection
$ ("<optionvalue= ' 1 ' >1111</option><optionvalue= ' 2 ' >2222</option>"). AppendTo ("#sel")// option to add a drop-down box
$ ("#sel"). empty ();//Empty drop-down box

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

In jquery, use $ ("#id") to get the input element of the page, which is equivalent to document.getElementById ("element") However, it is a jquery object instead of a DOM The element object. Value is a property of the DOM element object. Therefore, use $ ("#id"). Value cannot be taken as a value by the following method:

Value:

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

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

Assignment value:

$ ("#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. <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
  2. <HTML xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <Meta http-equiv= "content-type" content= "text/html; Charset=utf-8 " />
  5. <title> Untitled document </title>
  6. <mce:script src="jquery-starterkit/lib/jquery-1.3.2.min.js" mce_src= "jquery-starterkit/ Lib/jquery-1.3.2.min.js " type=" Text/javascript "></mce:script>
  7. </head>
  8. <body>
  9. <input type="text" id="dd" name="dd" value="DDS"/>dd
  10. <input name="RR" id= "rr" type= "Radio" value=" />ff"
  11. <input name="RR" id= "rr2" type="Radio" value="4" />55
  12. <input name="ff" type="checkbox" value="AA" />jgdg
  13. <input name="ff" type="checkbox" value="GD" />jgdg
  14. <select name="ss" id="ss" size="1">
  15. <option value= ""></option>
  16. <option value="8">d</option>
  17. <option value="2">g</option>
  18. </Select>
  19. <br/>
  20. <input type="button" id= "button" value="buttons One" />
  21. <input type="button" id="JJ" value="pushbutton two" />
  22. <br/>
  23. <div id="SSD">fgfoohello</div>
  24. </body>
  25. <mce:script language="javascript" type="Text/javascript"><!--
  26. $ (function () {
  27. $ ("#button"). Click (function () {
  28. Get value
  29. Alert ($ (' #dd '). Val ());//type=text
  30. Alert ($ (' input[name=rr][checked] '). Val ());//type=Radio
  31. Alert ($ (' input[name=ff][checked] '). Val ());//type=checkbox
  32. Alert ($ ("select[name=SS] option[selected]"). Val ());//select is equal to alert ($ ("#ss option[selected]"). Val ());
  33. Get text
  34. Alert ($ ("select[name=SS] option[selected]"). text ());//select
  35. Control
  36. /*//Disable #dd Disabled
  37. $ ("#dd"). attr ("Disabled", "disabled");
  38. Enable #dd unblock
  39. $ ("#jj"). Removeattr ("Disabled"); */
  40. $ (' input[name=RR] '). Get (0). checked = true;//The first radio is selected
  41. Alert ($ ("input[type=radio][value=)"). attr ("Checked", ' checked '));//value= 34 of Radio are selected
  42. Alert ($ ("input[type=checkbox][value=gd]"). attr ("Checked", ' checked '));//value= GD's checkbox is selected
  43. $ (' input[name=ff] '). Get (1). checked = true;//The first check is selected
  44. /*//option is selected according to the text of option
  45. count=$ ("#ss"). Find ("option"). Length;
  46. for (Var i=0;i<count;i++)
  47. {
  48. if ($ ("#ss"). Get (0). options[i]. Text = = ' d ')
  49. {
  50. $ ("#ss"). Get (0). Options[i]. selected = true;
  51. Break
  52. }
  53. } */
  54. //$ ("<option  Value= ' 1 ' >1111</ option><option < span class= "attribute" >value= ">22s22 </option> "). AppendTo (" #ss ");//add option  
  55. $ ("#ss option[value=8]"). Remove ("");//Remove <option value=' 8 '>d</ Option>
  56. $ ("#ss"). attr ("Value", ' 2 ');//SELECT option
  57. $ (' #ss ') [0]. SelectedIndex = 1;//Select option
  58. $ ("#ss"). empty ();//Clear All option
  59. /*//replacing text
  60. var $Thirdlink = $ ("#ssd");
  61. var linkText = $thirdLink. Text (). replace (' foo ', ' Bar ');
  62. $thirdLink. Text (LinkText); */
  63. });
  64. });
  65. --></mce:script>
  66. </html>

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.