[英]Validation of radio buttons with Javascript
我已經在這里梳理了大量的頁面,但仍然無法讓我的顯式驗證器工作。 基本上,當單擊提交按鈕時,我希望腳本驗證是否檢查了無線電,如果檢查了無線電。 如果未選中,我希望它發布警報消息。
大致我的HTML看起來像:
<form id="myForm" onsubmit = "check()">
<input type = "radio" name = "c" id = "1" value = "1" />
<input type = "radio" name = "c" id = "2" value = "2" />
<input type = "radio" name = "c" id = "3" value = "3" />
<input type = "submit" value = "Submit" />
我的JS頁面看起來像:
function check() {
var r = document.getElementsByName("c")
var c = 0
for(var i=0; i < r.length; i++){
if(c[i].checked) {
c = i; }
}
alert("please select radio");
}
試試這個
function check() {
var r = document.getElementsByName("c")
var c = -1
for(var i=0; i < r.length; i++){
if(r[i].checked) {
c = i;
}
}
if (c == -1) alert("please select radio");
}
這個
c[i].check
應該
c[i].checked
而你實際上並沒有對結果做任何事情,你只是總是提醒你。
<html>
<head>
<script language="javascript">
function check() {
chosen = ""
len = document.myform.chk.length
for (i = 0; i <len; i++) {
if (document.myform.chk[i].checked) {
chosen = document.myform.chk[i].value
}
}
if (chosen == "") {
alert("No Option selected");
return false;
}
else {
alert("option selected");
return true;
}
}
</script>
</head>
<body>
<form name="myform" onsubmit = "return check();">
<input type = "radio" name = "chk" id = "1" value = "1" >
<input type = "radio" name = "chk" id = "2" value = "2" >
<input type = "radio" name = "chk" id = "3" value = "3" >
<input type="submit" value="submit">
</form>
</body>
</html>
function ShowMsg() {
if (fnSpeciality() == false)
{
document.getElementById("myform").focus();
return false;
}
function fnSpeciality()
{
return fnRblfnSpeciality();
}
function fnSpeciality() {
return fnRblfnSpeciality();
}
function fnRblfnSpeciality() {
var list = document.getElementById('myform'); //Client ID of the radiolist
var inputs = list.getElementsByTagName("input");
var isItemChecked = false;
for (var i = 0; i < inputs.length; i++) {
var listItem = inputs[i];
if (listItem.checked) {
//alert(listItem.value);
isItemChecked = true;
break;
}
}
if (isItemChecked == false) {
if (isItemChecked =="") {
alert('Please select a speciality.');
return false;
}
// else return true;
}
return true;
}
CreditCard DebitCard現金
</td>
</tr>
使用單選按鈕列表..pls共享如果有此單選按鈕的java腳本驗證
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.