JavaScript多复选框验证不起作用
我有一张表格JavaScript多复选框验证不起作用,javascript,forms,checkbox,Javascript,Forms,Checkbox,我有一张表格 <form method="post" action="" onsubmit="validate()"> <input type="checkbox" name="del[]" value="1"> <input type="checkbox" name="del[]" value="2"> <input type="checkbox" name="del[]" value="3"> <input type="checkbox"
<form method="post" action="" onsubmit="validate()">
<input type="checkbox" name="del[]" value="1">
<input type="checkbox" name="del[]" value="2">
<input type="checkbox" name="del[]" value="3">
<input type="checkbox" name="del[]" value="4">
<input type="checkbox" name="del[]" value="5">
<button type="submit" class="btn btn-danger btn-lg">delete</button>
</form>
删除
我尝试用JavaScript进行复选框验证,如果人们没有选中复选框,它将显示一条消息,如果人们选中一个或多个复选框,
它将显示确认警报以确认提交。但我的JavaScript无法工作。表格将在未经验证的情况下提交
<script>
function validate() {
var checkbox=document.getElementsByName("del[]");
if (checkbox.checked == null || x == "") {
alert("Please select!");
var check=false;
return false;
}
if(check != false && !confirm('confirm submit?')){
e.preventDefault();
return false;
}
return true;
}
</script>
函数验证(){
var checkbox=document.getElementsByName(“del[]”);
如果(checkbox.checked==null | | x==“”){
警报(“请选择!”);
var检查=假;
返回false;
}
if(check!=false&&!confirm('confirm submit?')){
e、 预防默认值();
返回false;
}
返回true;
}
如何解决此问题?类似的操作将确保至少选中一个复选框
document.getElementById('myform')。onsubmit=function(e){
var checkbox=document.getElementsByName(“del[]),
我
选中的;
对于(i=0;i
删除
类似的操作将确保至少选中一个复选框
document.getElementById('myform')。onsubmit=function(e){
var checkbox=document.getElementsByName(“del[]),
我
选中的;
对于(i=0;i
删除
类似的操作将确保至少选中一个复选框
document.getElementById('myform')。onsubmit=function(e){
var checkbox=document.getElementsByName(“del[]),
我
选中的;
对于(i=0;i
删除
类似的操作将确保至少选中一个复选框
document.getElementById('myform')。onsubmit=function(e){
var checkbox=document.getElementsByName(“del[]),
我
选中的;
对于(i=0;i
删除
getElementsByName
返回对象的集合。该集合没有选中的属性,因此此操作失败:
var checkbox = document.getElementsByName("del[]");
if (checkbox.checked == null ... //error, no "checked" property
一个简单的替代方法是使用查找单个选中输入:
function validate() {
var checkbox= document.querySelector('input[name="del[]"]:checked');
if(!checkbox) {
alert('Please select!');
return false;
}
else return confirm('confirm submit?');
}
此外,请更改此项:
<form method="post" action="" onsubmit="validate()">
……为此:
<form method="post" action="" onsubmit="return validate()">
getElementsByName
返回对象的集合。该集合没有选中的属性,因此此操作失败:
var checkbox = document.getElementsByName("del[]");
if (checkbox.checked == null ... //error, no "checked" property
一个简单的替代方法是使用查找单个选中输入:
function validate() {
var checkbox= document.querySelector('input[name="del[]"]:checked');
if(!checkbox) {
alert('Please select!');
return false;
}
else return confirm('confirm submit?');
}
此外,请更改此项:
<form method="post" action="" onsubmit="validate()">
……为此:
<form method="post" action="" onsubmit="return validate()">
getElementsByName
返回对象的集合。该集合没有选中的属性,因此此操作失败:
var checkbox = document.getElementsByName("del[]");
if (checkbox.checked == null ... //error, no "checked" property
一个简单的替代方法是使用查找单个选中输入:
function validate() {
var checkbox= document.querySelector('input[name="del[]"]:checked');
if(!checkbox) {
alert('Please select!');
return false;
}
else return confirm('confirm submit?');
}
此外,请更改此项:
<form method="post" action="" onsubmit="validate()">
……为此:
<form method="post" action="" onsubmit="return validate()">
getElementsByName
返回对象的集合。该集合没有选中的属性,因此此操作失败:
var checkbox = document.getElementsByName("del[]");
if (checkbox.checked == null ... //error, no "checked" property
一个简单的替代方法是使用查找单个选中输入:
function validate() {
var checkbox= document.querySelector('input[name="del[]"]:checked');
if(!checkbox) {
alert('Please select!');
return false;
}
else return confirm('confirm submit?');
}
此外,请更改此项:
<form method="post" action="" onsubmit="validate()">
……为此:
<form method="post" action="" onsubmit="return validate()">
另一种简单的方法是在表单加载和单击提交按钮时创建并调用函数validate()
。
通过使用checked
属性检查复选框是否选中。
cbox[0]
有一个索引0
,用于使用name=“del[]”访问第一个值(即1)
您可以执行以下操作:
函数验证(){
var cbox=document.forms[“myForm”][“del[]”;
如果(
cbox[0]。选中==false&&
cbox[1]。选中==false&&
cbox[2]。选中==false
) {
警惕(“请检查一下”);
返回false;
}否则(确认(“确认提交?”){
警报(“成功提交”);
返回true;
}
}
1.
2.
3.
4.
5
另一种简单的方法是在表单加载和单击提交按钮时创建并调用函数validate()
。
通过使用checked
属性检查复选框是否选中。
cbox[0]
有一个索引0
,用于使用name=“del[]”访问第一个值(即1)
您可以执行以下操作:
函数验证(){
var cbox=document.forms[“myForm”][“del[]”;
如果(
cbox[0]。选中==false&&
cbox[1]。选中==false&&
cbox[2]。选中==false
) {
警惕(“请检查一下”);
返回false;
}否则(确认(“确认提交?”){
警报(“成功提交”);
返回true;
}
}
1.
2.
3.
4.
5
另一种简单的方法是在