[Development Notes]-jquery obtains the value of the radio selected item

Source: Internet
Author: User
 <  Title  > </  Title  >      <  Script  SRC  = "JS/jquery-1.7.2.min.js"  > </  Script  >      <  Script  Type  = "Text/JavaScript"  > $ (  Function  (){  //  No selected by default              //  First, judge whether radio has selected items and then obtain the selected values.  $ (  "  # Btnclick  " ). Click (  Function  (){  //  Obtains the Value Attribute of the selected item.                  VaR  Chkradio  =  $ (  '  Input: Radio [name = "list"]: checked '  ). Val ();  If  (Chkradio  =   Null  ) {Alert (  "  No selected items  "  ); Return   False  ;}  Else  {Alert (chkradio );}});  //  Selected by default              //  Obtains the value of a selected item. Generally, the Traversal method is used to determine which item is selected and obtain the value of its value attribute.  $ ( "  # Btnclick2  "  ). Click (  Function  (){  VaR  CHK2  =  Getradiovalue (  " List2  "  ); Alert (CHK2 );});  Function  Getradiovalue (radioname ){  VaR  Chkradio  =  Document. getelementsbyname (radioname );  For (  VaR  I  =   0  ; I  <  Chkradio. length; I  ++  ){  If (Chkradio [I]. Checked)  Return  Chkradio [I]. Value ;}}})  </  Script  >  </  Head  >  <  Body  >      <  Div >          <  Input  Type  = "Radio"  Name  = "List"  Value  = "Very satisfied"   />  Very satisfied  <  Input  Type  = "Radio"  Name  = "List" Value  = "Satisfied"   />  Satisfied  <  Input  Type  = "Radio"  Name  = "List"  Value  = "Not satisfied"   />  Unsatisfied  <  Input  Type = "Radio"  Name  = "List"  Value  = "Very poor"   />  Very poor  </  Div  >      <  BR  />      <  Div  >          < Input  Type  = "Radio"  Name  = "List2"  Value  = "Very satisfied 2"  Checked  = "Checked"   />  Very satisfied 2  <  Input  Type  = "Radio"  Name  = "List2" Value  = "Satisfied 2"   />  Satisfied 2  <  Input  Type  = "Radio"  Name  = "List2"  Value  = "Not satisfied 2"   />  Unsatisfied 2  <  Input  Type = "Radio"  Name  = "List2"  Value  = "Very bad 2"   />  Very bad 2  </  Div  >      <  Input  Type  = "Button"  Value  = "OK"  ID = "Btnclick"   />      <  Input  Type  = "Button"  Value  = "OK 2"  ID  = "Btnclick2"   />  </  Body  >  </  Html  > 

Demo:

1. If no default item is selected, the result is null:

2. If no selected item is selected by default, obtain the value of the selected item:

3. If a selected item exists, obtain the value of the selected item:

Indicate the source for reprinting.

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.