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

May 16, 2016 pm 06:47 PM
form Radio Single choice

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

Copy code The code is as follows:

<script type="text/javascript">
// Description: Use Javascript to verify the radio value in the form
// Author: CodeBit
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>

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 of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement multi-select, radio-select and other form components in Vue? How to implement multi-select, radio-select and other form components in Vue? Jun 25, 2023 pm 09:49 PM

How to implement multi-select, radio-select and other form components in Vue?

60 years in the making: NASA confirms hypothesis on Earth\'s global electric field 60 years in the making: NASA confirms hypothesis on Earth\'s global electric field Aug 31, 2024 pm 09:35 PM

60 years in the making: NASA confirms hypothesis on Earth\'s global electric field

BinaryX is renamed FORM again, and the FOUR it gives to the community is about to soar? BinaryX is renamed FORM again, and the FOUR it gives to the community is about to soar? Mar 04, 2025 pm 12:00 PM

BinaryX is renamed FORM again, and the FOUR it gives to the community is about to soar?

Can there be multiple forms in html5? Can there be multiple forms in html5? Aug 01, 2022 pm 05:28 PM

Can there be multiple forms in html5?

What is the tag that defines the form in html5 What is the tag that defines the form in html5 Jul 26, 2022 pm 04:26 PM

What is the tag that defines the form in html5

Comprehensively organize elements related to form forms! Comprehensively organize elements related to form forms! Aug 05, 2022 am 11:45 AM

Comprehensively organize elements related to form forms!

What should I do if the form cannot be submitted to php? What should I do if the form cannot be submitted to php? Dec 01, 2022 am 09:08 AM

What should I do if the form cannot be submitted to php?

php记录搜索引擎蜘蛛爬行记录代码 php记录搜索引擎蜘蛛爬行记录代码 Jun 13, 2016 am 10:08 AM

php记录搜索引擎蜘蛛爬行记录代码

See all articles