javascript判断不能为空!
在一个表单提交中,我想限制多选按纽或但选按纽必须要选择,请问怎么实现,希望详细点,提供代码,完成后马上给分!
------解决方案--------------------//差不多这样,没测试
var flag=false
var objs=document.getElementsByName( "你的name ")
for(var i=0;i <objs.length;i++)
{
if(objs[i].checked)
{
flag=true;
break;
}
}
if(!flag)
{
alert( "没选 ")
return false
}
else
{
form1.submit()
}
------解决方案--------------------给你写了一个
<script>
function check()
{
var rdo=document.getElementsByName( "rdo ");
var num=false;
for(var i=0;i <rdo.length;i++)
{
if(rdo[i].checked)
{
num=true;
}
}
if(!num)
{
alert( "请选择一项 ");
}
}
</script>
<body>
<form name= "form1 " method= "post " action= " ">
<input type= "radio " name= "rdo "> 11111111
<input type= "radio " name= "rdo "> 22222222
<input type= "radio " name= "rdo "> 33333333
<input type= "radio " name= "rdo "> 44444444
<input type= "button " value= "检查 " onclick= "check(); "/>
</form>
</body>
------解决方案-------------------- <script>
function check()
{
var objIput = document.getElementsByTagName( "INPUT ");
for(var i=0,j=objIput.length;i <j;i++)
{
if(objIput[i].checked)
return( "OK ");
}
return( "请选择一个 ")
}
</script>
<input type= "radio " name= "test1 "> test1_1
<input type= "radio " name= "test1 "> test1_2
<input type= "checkbox " name= "test2 "> test2_1
<input type= "checkbox " name= "test3 "> test2_2
<input type= "button " value= "checkValue " onclick= "alert(check()) "/>