Home >Web Front-end >JS Tutorial >Use Javascript to verify the radio value in the form_form effects

Use Javascript to verify the radio value in the form_form effects

WBOY
WBOYOriginal
2016-05-16 18:47:001209browse

This article introduces a more general method of obtaining radio values, hoping to be useful to novices.

Copy code The code is as follows:



radio is the same as checkbox, both have the same name and multiple values. When getting the radio value, we cannot According to the method of ordinary text box .value, it is necessary to determine which one is selected.

When a group of radios has multiple options, we can loop through radio[i] to determine whether an option is selected to return a value, but when a group of radios has only one option, get The method of value has changed again. In the code, return (radio.checked)?radio.value:''; is used to directly determine whether it is selected, and then returns the corresponding value.

The parameter passed in by the above code is the radio object, such as:
Copy the code The code is as follows:

var radioTest = document.forms['testForm'].elements['radioTest'];
if (getRadioValue(radioTest) == '')
{ ...... }

Perform the operation you want based on the judgment result.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn