1
我使用php处理表单,我也想在客户端添加验证。我阅读about.com有关使用javascript验证单选按钮的教程,并根据其指导原则设置此示例。验证单选按钮
a)为什么不显示警报消息?
b)我如何将警报消息替换为一些innerHtml文本?
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html><head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type"><title>validateradio</title>
<script type="text/javascript">
function valButton(btn) {
var cnt = -1;
for (var i=btn.length-1; i > -1; i--) {
if (btn[i].checked) {cnt = i; i = -1;}
}
if (cnt > -1) return btn[cnt].value;
else return null;
}
function valButton2(form){
var btn=valButton(form.group1);
if (btn==null){alert('no radio button selected');}
else alert('button value' + btn + 'selected');
}
</script>
</head>
<body style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);" alink="#000088" link="#0000ff" vlink="#ff0000"><br>
<form name="myform">
<label for="r1"><input type="radio"
name="group1" id="r1" value="1" /> button one</label>
<label for="r2"><input type="radio"
name="group1" id="r2" value="2" /> button two</label>
<label for="r3"><input type="radio"
name="group1" id="r3" value="3" /> button three</label>
<input type="submit" name="submitit" onclick="valbutton2(myform);" value="Validate" />
</form>
</body></html>
谢谢,这解决了我的第一个问题。 – andandandand 2010-08-28 00:21:30
顺便说一句,你用编辑器来捕捉?我正在研究Ubuntu的gedit,并希望能够帮助您发现语法问题。 – andandandand 2010-08-28 00:23:18
查看我的更新以获取文本的可能方式。我没有使用编辑器,只是通过查找来捕捉它。 – Bryan 2010-08-28 00:43:49