javascript check radio buttons automatically

前端 未结 2 1900
滥情空心
滥情空心 2021-01-11 23:28

I wanna check radio buttons automatically: I tried this code but it does not work: Radio buttons have 3 different values, I wanna select the radio button with value \'clean\

2条回答
  •  清酒与你
    2021-01-12 00:28

    Give your radio buttons "names" would make things a lot easier

    
    
    
    var elements = document.getElementsByName('myradios');
    for (i=0;i

    Working example : http://jsfiddle.net/Dwzc9/

    Updated

    getElementsByName doesn't seem to be supported in all IE versions ... so you could use the following based on your original example :

    var allElems = document.getElementsByTagName('input');
    for (i = 0; i < allElems.length; i++) {
        if (allElems[i].type == 'radio' && allElems[i].value == 'clean') {
            allElems[i].checked = true;
        }
    }
    

    Working example : http://jsfiddle.net/Dwzc9/2/

提交回复
热议问题