【发布时间】:2014-06-03 10:12:56
【问题描述】:
我有多个名为 optionRadios1 到 5 的分组单选按钮。我想通过 javascript 验证它们,但我的验证代码不起作用,但仍继续提交表单。
这是我的代码:
function submitForm(){
var counter = "<?php echo $count;?>"; //$count is the total number of grouped radio buttons
for(j=1;j<=counter;j++) {
var radioname = "optionRadios" + j;
var elem=document.forms['test-form'].elements[radioname];
len=elem.length-1;
chkvalue='';
for(i=0; i<=len; i++){
if(elem[i].checked){chkvalue=elem[i].value;}
}
if(chkvalue=='') {
alert('There are still unanswered questions. ' + radioname);
return false;
}else {
$('#test-form').submit();
}
}
}
这是应该使用的形式:
<form action="#" method="post" id="test-form">
//lots of codes here
<button type="submit" class="btn btn-primary" onclick="return submitForm();" id="next">Next</button>
</form>
【问题讨论】:
-
想要澄清“我的代码不起作用”?
-
抱歉,我的 javascript 验证码不起作用
-
nmean not working 是什么意思?
-
好的,怎么它不起作用?它不应该做什么,它不做什么它应该?您可以在JS Fiddle 或类似网站上设置一个现场演示并说明缺陷吗?
-
still continues to submit the form那你怎么防止呢???控制台中的任何错误?在你这边调试呢?!
标签: javascript php jquery