[英]Issue in firing alert message in javascript for dynamically generated checkboxes
I am facing issue while doing checkbox validation in my jsp using javascript. 我在使用JavaScript在jsp中进行复选框验证时遇到问题。
The checkbox generated inside my form, have dynamic ids: 我的表单内部生成的复选框具有动态ID:
<form action="displayInstallType" method="get" onsubmit="return checkbox()">
<c:forEach var="lineInstallType" items="${InstallTypeList.listOfInstallType}">
<input type="checkbox" name="${lineInstallType}" value="${lineInstallType}" class="InstallType"/> ${lineInstallType}
</c:forEach>
</form>
In my javaScript, I am trying to do: 在我的javaScript中,我正在尝试执行以下操作:
function checkbox() {
var InstallTypeChecked=0;
var list=document.getElementsByClassName("InstallType");
for(var i = 0; i < list.length; i++){
var elem = document.getElementById(list[i].id);
if(elem!=null){
InstallTypeChecked=1;
}
}
if (InstallTypeChecked == 0 ) {
alert('One or more radio buttons/checkboxes are not selected.Please select them first.');
return false;
} else {
return true;
}
}
But I am not getting any alert if I click submit button without selecting any checkbox. 但是,如果我单击提交按钮而不选择任何复选框,则不会收到任何警报。
Kindly help as I am new to javascript. 请帮忙,因为我是javascript新手。 Any help will be appreciated. 任何帮助将不胜感激。
try modifying your code as below, 尝试如下修改您的代码,
function checkbox() {
var InstallTypeChecked=0;
var list=document.getElementsByClassName("InstallType");
for(var i = 0; i < list.length; i++){
if(list[i].checked){
InstallTypeChecked=1;
}
}
if (InstallTypeChecked == 0 ) {
alert('One or more radio buttons/checkboxes are not selected.Please select them first.');
return false;
} else {
return true;
}
}
You may try this also: 您也可以尝试以下操作:
function checkbox()
{
var InstallTypeChecked = 0;
var list = document.getElementsByClassName("InstallType");
for(var i = 0; i < list.length; i++)
{
if(list[i].checked)
{
InstallTypeChecked = 1;
break;
}
}
if(InstallTypeChecked == 0)
{
alert('One or more radio buttons/checkboxes are not selected.Please select them first.');
return false;
}
return true;
}
Try this 尝试这个
function checkbox() {
var list=document.getElementsByClassName("InstallType");
for(var i = 0; i < list.length; i++){
if(list[i].checked){
return true;
}
}
alert('One or more radio buttons/checkboxes are not selected.Please select
return false;
}
And give unique id to your input like 并给您的输入唯一的ID,例如
<input id="${lineInstallType}" type="checkbox" name="${lineInstallType}" value="${lineInstallType}" class="InstallType"/>
if ${lineInstallType} this is not unique then use i with increment in iteration for id. 如果$ {lineInstallType}不是唯一的,则将i与ID一起在迭代中递增。
Here is the most condensed version assuming I understood what you want 假设我了解您想要的内容,这是最简洁的版本
function checkbox() {
var list = document.getElementsByClassName("InstallType");
for(var i = 0; i < list.length; i++) {
if(list[i].checked) return true;
}
alert('One or more radio buttons/checkboxes are not selected.Please select them first.');
return false;
}
Alternative for not too old browsers 对于不太旧的浏览器的替代
function checkbox() {
if (document.querySelectorAll('option:checked').length==0) {
alert('One or more radio buttons/checkboxes are not selected.Please select them first.');
return false;
}
return true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.