简体   繁体   中英

handling events to multiple radio buttons

I have multiple radio button in my page with name ,"p1radio","p2radio","p3radio"..etc..When I click any of the radio button, I need a set of actions to be taken place.

html

<td><input type="radio" name="p1radio"/><

/td>
<td><input type="radio" name="p21radio"/></td>
<td><input type="radio" name="p31radio"/></td>
<td><input type="radio" name="p41radio"/></td>

Question: Is there a way to write a generic event handler for all these radio buttons,since only the first 2 bytes - p1, p2...varies in these buttons...

You could use something like this :

function clickFunc(){};
var radios = ['p2','p3','p4'];
for(var i in radios) document.getElementById(radios[i]+'1radio').onclick = clickFunc;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM