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

<script language="javascript">
function check(form){
if (form.name.value==""){
alert("请输入登录名称!");form.name.focus();return false;
}
if (form.pwd.value==""){
alert("请输入密码!");form.pwd.focus();return false;
}

if(form.ident[0].checked){
  document.form1.action="manager_Login";
}else{
  document.form1.action="reader_Login";


}

</script>

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

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
        <title>text</title>
        <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获取选中值,如:
<asp:RadioButtonList ID="rblID" runat="server" RepeatColumns="7" RepeatLayout="Flow">
       <asp:ListItem Value="1" Selected="True">选项一</asp:ListItem>
       <asp:ListItem Value="2">选项二</asp:ListItem>
       <asp:ListItem Value="3">选项三</asp:ListItem>
</asp:RadioButtonList>
注意各选项radio.ID的html生成规则分别为:rblID_0、rblID_1、rblID_2。即等同于:
<input id="rblID_0" type="radio" name="rblID" value="1" checked="checked" />
<input id="rblID_1" type="radio" name="rblID" value="2" />
<input id="rblID_2" type="radio" name="rblID" value="3" />

猜你喜欢

转载自blog.csdn.net/jeffyu328/article/details/18080677
今日推荐