在复选框列表中,我必须为该javascript选择至少一项 [英] In checkbox list i have to select atleast one item for that javascript

查看:74
本文介绍了在复选框列表中,我必须为该javascript选择至少一项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我已经在javascript中编写了一个函数

Hi,

i have written one function in javascript

function validate(source, args) {
           var chkcontrol = document.getElementById("<%= ChklistDomain.ClientID %>");
           if (!chkcontrol.checked) {
              alert("Please select ateleast one Domain")
               return false;
            }



当我没有选择复选框列表中的任何项目时它的工作,但当我在复选框列表中选择项目时,它也显示警报消息.我必须更改的内容请帮助我.



its working for when i didnt select any items in checkboxlist but it also displaying alert msg when i selected items in checkbox list. what i have to change please help me.

推荐答案

您必须使用for循环检查checkboc列表.

我的意思是:您必须选中列表中的单个复选框,如果选中了一个复选框,则可以返回true,并且如果循环执行完毕而不执行该return语句,则表示未选中任何内容.因此,在for之外返回false循环.

在您的代码中,您正在检查是否已选中所有复选框.因此,只有在选中所有复选框后,它才能满足您的条件.

您的代码应如下所示:

you have to check the checkboc list by using a for loop.

what i mean is: you have to check single check boxes in the list and if one is checked then you can return true and if the loop is completed without executing that return statement it means nothing is checked.so you have return false outside the for loop.

In your code you are checking whether all the checkboxes are checked or not.So,it will satisfy your condition only when all the checkboxes are checked.

Your code should be like this:

for(var i=0;i<n;i++)>
{
    if(chkcontrol[i].checked)
         return true;
}
return false;




评价答案是否有用...




rate the answer if it is useful...


这篇关于在复选框列表中,我必须为该javascript选择至少一项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆