Javascript verification single-choice (radio) value and multiple-choice box

Source: Internet
Author: User

 

 

<SCRIPT type = "text/JavaScript">
// Description: use JavaScript to verify the single-choice (radio) value in the form (Form ).

 

 

 

Function getradiovalue (Radio)
{
If (! Radio. Length & radio. type. tolowercase () = 'Radio ')
{Return (radio. Checked )? Radio. Value :'';}
If (radio [0]. tagname. tolowercase ()! = 'Input' |
Radio [0]. type. tolowercase ()! = 'Radio ')
{Return '';}
VaR Len = radio. length;
For (I = 0; I <Len; I ++)
{
If (radio [I]. Checked)
{
Return radio [I]. value;
}
}
Return '';
}
</SCRIPT>

Like the checkbox, Radio has the same name and multiple values. When obtaining the radio value, we cannot follow the common text box. value method, but determine which one is selected.

When a set of Radio has multiple options, we can use a loop to determine whether an option is selected to return the value in the form of radio [I, however, when a set of Radio has only one option, the way to get the value changes. In the code, return (radio. checked )? Radio. Value: ''; in this way, you can directly determine whether to select and then return the corresponding value.

The parameters passed in the above Code are radio objects, such:

The Code is as follows: var radiotest = Document. Forms ['testform']. elements ['radiotest'];
If (getradiovalue (radiotest) = '')
{......}

Perform the operations you want based on the judgment results.

 

 

Same as a single regionJavascriptWhen you verify the value of the Multi-choice box (checkbox) in the form (Form), you will encounter problems becauseCheckboxIt is very different from a common text box in obtaining values. This article introduces a more common way to obtain values.CheckboxValue method.

Javascript:

 
  1.  
  2. <SCRIPT type = "text/JavaScript">
  3.  
  4. // Description: use JavaScript to verify the value of the Multi-choice box (checkbox) in the form (Form ).
  5.  
  6.  
  1. Function getcheckboxvalue (checkbox)
  2. {
  3. If (! Checkbox. Length & checkbox. type. tolowercase () =
    'Checkbox ')
  4. {Return
    (Checkbox. Checked )? Checkbox. Value :'';
    }
  5. If (checkbox [0]. tagname. tolowercase ()
    ! = 'Input' |
  6. Checkbox [0]. type. tolowercase ()! =
    'Checkbox ')
  7. {Return
    '';}
  8.  
  9. Var val = [];
  10. VaR Len = checkbox. length;
  11. For (I = 0; I <Len; I ++)
  12. {
  13. If (checkbox [I]. Checked)
  14. {
  15. Val [Val. Length] = checkbox [I]. value;
  16. }
  17. }
  18. Return (Val. Length )? VAL :'';
  19. }
  20.  
  21. </SCRIPT>
  22. AndRadioAllNameSame. There are multiple values.CheckboxValue, we cannot follow the common text box. ValueTo determine which one is selected.

    When a groupCheckboxWhen there are multiple options, we can useCheckbox [I]To determine whether an option is selected to return the value.CheckboxWhen there is only one option, the way to get the value changes.(Checkbox. Checked )? Checkbox. Value :'';And then return the corresponding value.

    The parameters passed in the above Code areCheckboxObjects, such:

    VaRCheckboxtest=Document. Forms ['testform']. elements ['checkboxtest'];
    If(Getcheckboxvalue (checkboxtest)='')
    {......}

    Perform the operations you want based on the judgment results.

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.