老记不住这个js判断radio的写法

/* *********************************************还可以如下:(转自http://blog.sina.com.cn/s/blog_77dbebbb010124jp.html)******************************************************** */

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
        <title>texttitle>
        <script>
            var chk = 0;
            window.onload
=function (){
                var chkObjs = document.getElementsByName("radio");
                for(var i=0;i<chkObjs.length;i++){
                    if(chkObjs[i].checked){
                        chk
 = i;
                        break;
                    }
                }
            }
            function check_radio(){
                var chkObjs = document.getElementsByName("radio");
                for(var i=0;i<chkObjs.length;i++){
                    if(chkObjs[i].checked){
                        if(chk == i){
                            alert(
"radio值没有改变不能提交");
                            break;
                        }
                    }
                }
            }
        script>
    head>

    <body>
        <form action='' method='post' onsubmit='javascript:return check_radio()'>
            <input type='radio' value='1' name='radio' checked='checked'>一;
            <input type='radio' value='2' name='radio'>二;
            <input type='radio' value='3' name='radio'>三;
            <input type='radio' value='4' name='radio'>四;
            <input type='radio' value='5' name='radio'>五;


            <input type=submit value=sub >
        form>
    body>
html>

=============================================

另外,如果使用.net控件,仍然可以使用JS获取选中值,如:

       选项一
       选项二
       选项三

注意各选项radio.ID的html生成规则分别为:rblID_0、rblID_1、rblID_2。即等同于:



你可能感兴趣的:(js)