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