How to dynamically change radio status through js (detailed tutorial)

亚连
Release: 2018-06-04 10:48:55
Original
2690 people have browsed it

Below I will share with you a js method to dynamically change the radio status. It has a good reference value and I hope it will be helpful to everyone.

h5's radio comes with a selected state change, but if the built-in state cannot meet your needs, you need to implement it yourself.

The code is as follows:

h5 partial code

Copy after login

CSS code

Copy after login

JS method code

Copy after login

The effect is as follows

What is implemented here is the dynamic display and hiding of the top boder and the default round button on the left side of the radio is set to hidden. If you want the button not to be hidden, you need to make the following modifications

Copy after login

That is, add an img in front of each raid type input (note the difference between selected and unselected), and make the following modifications to the JS change method

The length of
var radio = document.getElementsByName("parent_radio"); var img = document.getElementsByName("image"); /*用ByName是为了取到所有的radio*/ var radioLength = radio.length; for(var i = 0;i < radioLength;i++) { if(radio[i].checked) { img[i].src = "images/delate_choose.png"; radio[i].parentNode.setAttribute('class', 'active'); }else { img[i].src = "images/delate_no_choose.png"; radio[i].parentNode.setAttribute('class', ''); } }
Copy after login

img must be the same as the length of radio, so you can only take one length.

The effect is as follows:

#The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

How to use Vue-Router patterns and hooks (detailed tutorial)

Using vue- in vue-cli Router builds the bottom navigation bar (detailed tutorial)

How to use select to load data and select the default value in AngularJS?

The above is the detailed content of How to dynamically change radio status through js (detailed tutorial). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!