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
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot tools Tags

Hot Article
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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

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

Can there be multiple forms in html5?

What is the tag that defines the form in html5

Comprehensively organize elements related to form forms!

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