<script language="javascript">
function check(){
window.alert(document.getElementById("ra2").checked);
}
</script>
<form name="form1" id="form1" method="post" action="" onsubmit="return check()">
<input type="radio" name="ra" id="ra1" value="1" />111<br />
<input type="radio" name="ra" id="ra2" value="2" />222<br />
<input type="radio" name="ra" id="ra3" value="3" />333<br />
<input type="radio" name="ra" id="ra4" value="4" />444<br />
<input type="radio" name="ra" id="ra5" value="5" />555<br />
<input type="submit" name="submit" id="submit" />
</form>像这样?
<script language="javascript">
function check(){
var ra1 = document.getElementById("ra1").checked;
var ra2 = document.getElementById("ra2").checked;
if (ra1 == false && ra2 == false){
window.alert("必须选择一个选项");
return false
}
return true;
}
</script>
<form name="form1" id="form1" method="post" action="" onsubmit="return check()">
<input type="radio" name="ra" id="ra1" value="1" />111<br />
<input type="radio" name="ra" id="ra2" value="2" />222<br />
<input type="submit" name="submit" id="submit" />
</form>那就这样吧?
<html>
<head>
<script>
function check()
{
alert('aaa');
if(document.getElementById("ra1").checked)
{
alert('可以提交了');
return true;
}
return false;
}
</script>
</head>
<body>
<form name="form1" id="form1" method="post" action="" onsubmit="return check()">
<input type="radio" name="ra" id="ra1" value="1" />111
<input type="radio" name="ra" id="ra2" value="2" />222<br />
<input type="submit" name="submit" id="submit" />
</form>
</body>
</html>
<script language="javascript">
function check(){
var ra = document.getElementsByName("ra");
for (var i=0;i<ra.length;i++){
if (ra[i].checked == true){
return true;
}
}
window.alert("必须选择一个");
return false;
}
</script>
<form name="form1" id="form1" method="post" action="" onsubmit="return check()">
<input type="radio" name="ra" id="ra1" value="1" />111<br />
<input type="radio" name="ra" id="ra2" value="2" />222<br />
<input type="radio" name="ra" id="ra3" value="3" />333<br />
<input type="submit" name="submit" id="submit" />
</form>这个就可以了,不需要用ID
用name,不止两个选项也可以用
<input type="radio" name="YouSex" value="女" />我的是这样,同名。没有ID。你上边写了一个ID几,用For来一个一个检测。 if(document.getElementsByName("YouSex").Checked != true)
{
window.alert("请您选择自己的性别!");
return false;
}
选了也是弹出来。
function check(){
var ra = document.getElementsByName("ra");
for (var i=0;i<ra.length;i++){
if (ra[i].checked == true){//这里不是吗?一个一个对比。
return true;
}
}
window.alert("必须选择一个");
return false;
}
</script>
<form name="form1" id="form1" method="post" action="" onsubmit="return check()">
<input type="radio" name="ra" id="ra1" value="1" />111<br />
<input type="radio" name="ra" id="ra2" value="2" />222<br />
<input type="radio" name="ra" id="ra3" value="3" />333<br />
<input type="submit" name="submit" id="submit" />
</form>我如同你这样写不行啊。
<input name="YouSex" type="radio" value="男" />
男
<input name="YouSex" type="radio" value="女" />
女</label></td>
用我这段来写。
<script language="javascript">
function check(){
var ra = document.getElementsByName("ra");
for (var i=0;i<ra.length;i++){
if (ra[i].checked == true){
return true;
}
}
window.alert("必须选择一个");
return false;
}
</script>
<form name="form1" id="form1" method="post" action="" onsubmit="return check()">
<input type="radio" name="ra" value="1" />111<br />
<input type="radio" name="ra" value="2" />222<br />
<input type="radio" name="ra" value="3" />333<br />
<input type="submit" name="submit" id="submit" />
</form>我把ID删了,一样可以用,IE6和FF下均正常
ra[i].checked == true,这里的ra[i]是数组,不是ID。这整个过程和ID无关。
当有一个被选中时,就可以提交,如果都没被选中就不能提交
<script language="javascript">
function check(){
var YouSex = document.getElementsByName("YouSex");
for (var i=0;i<YouSex.length;i++){
if (YouSex[i].checked == true){
return true;
}
}
window.alert("必须选择一个");
return false;
}
</script>
<form name="form1" id="form1" method="post" action="" onsubmit="return check()">
<input name="YouSex" type="radio" value="男" />
男
<input name="YouSex" type="radio" value="女" />
女
<input type="submit" name="submit" id="submit" />
</form>这样不是蛮正常的么
var YouSex = document.getElementsByName("YouSex")
for(var nIndex; nIndex < YouSex.length; nIndex++)
{
if(YouSex[nIndex].checked == true)
{
return true;
}
document.alert("请您填写自己的性别!");
return false;
}不成功。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<script language="jscript">
function Check()
{
var YouName = document.getElementsByName("YouSex");
for(var i; i < YouName.lenght;i++)
{
if(YouName[i].checked == true)
{
return true;
}
window.alert("");
return false;
}
}
</script>
<body>
<form id="form1" name="form1" method="post" action="" onsubmit="return Check()">
<label>
<input type="radio" name="YouName" value="radiobutton" />
</label>
<label>
<input type="radio" name="YouName" value="radiobutton" />
</label>
<label>
<input type="submit" name="Submit" value="提交" onclick="return Check()" />
</label>
</form>
</body>
</html>错误。
function Check()
{
var YouSex = document.getElementsByName("YouSex");
for(var i=0; i < YouSex.length;i++)
{
if(YouSex[i].checked == true)
{
return true;
}
}
window.alert("");
return false;
}
</script>
<body>
<form id="form1" name="form1" method="post" action="" onsubmit="return Check()">
<label>
<input type="radio" name="YouSex" value="radiobutton" />
</label>
<label>
<input type="radio" name="YouSex" value="radiobutton" />
</label>
<label>
<input type="submit" name="Submit" value="提交" onclick="return Check()" />
</label>
</form>
你代码写错。
你的单选框的name是YouName,而你的JS代码里要读的是YouSex,这就会引起读不到值。
for 循环里,var i之后没有对i进行赋值,应该先赋0,即var i=0
window.alert("");return false;不应该放在for循环里。不然每次循环都会判断一次,如果你选中的是第二个选项,它循环到第一选项的时候,第一选项是没被选中的,这时就已经返回了false,无法提交。
得了,太急了,所以看以没有看。越做越乱。
多谢!