[英]How do I make sure that at least one checkbox is checked?
I am using checkboxes whose values is coming from database. 我正在使用其值来自数据库的复选框。 Its name is same but name is fetching like: 它的名字是一样的,但是名字是这样的:
<input type="checkbox" id="chkBankServices" name="<%=bs.getServiceID()%>">
<%=bs.getServiceDesc()%>
through this i am getting the values from the database. 通过这个我从数据库中获取值。 Now i have to validate that at least one checkbox should be selected.. 现在我必须验证应该选择至少一个复选框..
If any one can help me i shall be thankful to u. 如果有人可以帮助我,我将感谢你。 If i am giving like this the javascript code: 如果我给这样的javascript代码:
var services = document.getElementsById( 'chkBankServices' );
if(!(services[0].checked) &&
!(services[1].checked) &&
!(services[2].checked) &&
!(services[3].checked) &&
!(services[4].checked) &&
!(services[5].checked) &&
!(services[6].checked) &&
!(services[7].checked) &&
!(services[8].checked))
{
alert( "Please select at least one service to submit." );
return false;
}
It's not giving any alert message. 它没有给出任何警告信息。 Is anything wrong in that. 这有什么不对的。 Plz help me... Thanks in advance Plz帮助我......提前谢谢
在jQuery中:
alert( $("#chkBankServices input[type=checkbox]:checked").length > 0 );
Try this: 尝试这个:
var services = document.getElementById( 'chkBankServices' );
var checkboxes = services.getElementsByTagName('input');
var checked = false;
for (var i=0,i0=checkboxes.length;i<i0;i++)
if (checkboxes[i].type.toLowerCase()=="checkbox")
{
if (checkboxes[i].checked) checked = true;
}
and then: 接着:
if (!checked)
{
alert('Not checked');
return false;
}
There is no getElementsById method, since there should only be one element with a given id. 没有getElementsById方法,因为只应该有一个具有给定id的元素。 Perhaps you meant to use getElementsByName ? 也许你打算使用getElementsByName ? This allows multiple elements to be returned. 这允许返回多个元素。
As this is really a client side issue, it would help if you could post a sample of the generated HTML, and we can guide you further. 由于这确实是客户端问题,如果您可以发布生成的HTML示例,我们可以为您提供更多指导。
您是否检查了渲染的来源以确保您的复选框被赋予了预期的名称?
I don't know if this will help you with your specific problem, but your code would be easier to read if you avoided the massive if block and used a loop instead: 我不知道这是否会帮助您解决您的具体问题,但如果您避免使用大量if块并使用循环代码,则代码将更容易阅读:
var checked = false;
for(var i=0;i<services.length;i++){
if(services[i].checked){
checked = true;
}
}
if(!checked){
alert( "Please select at least one service to submit." );
return false;
}
Looking at your code, I'm betting you have that checkbox in a repeater of some sort and are creating multiple checkboxes with the same ID which is invalid html. 看看你的代码,我打赌你在某种转发器中有那个复选框,并创建了多个具有相同ID的复选框,这是无效的html。 I would wrap it in a div/span or something with an id like below: 我会将它包装在div / span或具有如下id的内容中:
if (!isSomethingChecked()) {
alert( "Please select at least one service to submit." );
return false;
}
function isSomethingChecked() {
var parent = document.getElementById("chkBankServices");
for (var child in parent.childNodes) {
var node = parent.childNodes[child];
if (node && node.tagName === "INPUT" && node.checked) {
return true;
}
}
return false;
}
I assumed the HTML looks like : 我假设HTML看起来像:
<div id="chkBankServices">
<input type="checkbox" id="Checkbox1" />
<input type="checkbox" id="Checkbox2" checked="checked"/>
<input type="checkbox" id="Checkbox3" checked="checked"/>
<input type="checkbox" id="Checkbox4" />
<input type="checkbox" id="Checkbox5" />
<input type="checkbox" id="Checkbox6" />
<input type="checkbox" id="Checkbox7" />
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.