简体   繁体   English

在检查我的复选框数组是否全部未选中后,如何显示此警告框

[英]How can i display this alert box after checking if my array of checkbox is all not checked

Hi guys i am creating a validation for checking if the user has selected at least one stand by checking it, if no stands are selected then an alert box should display telling the user to select at least one stand. 大家好我正在创建一个验证,检查用户是否通过检查选择了至少一个支架,如果没有选择支架,则应显示警告框,告诉用户至少选择一个支架。 my javascript function is triggered onsubmit. 我的javascript函数是在提交时触发的。

my code is 我的代码是

 function checkstanddocument(){ 
 ab = document.getElementById("repo_document_form").elements[['stands[]']];   
  for (var i = 0; i <= ab.length; i++){

    if (ab[i].checked == true){
      return true
    }    
 }  
   alert("Please select at least one stand");
   return false
}

I thought the system will only proceed to the alert box if no checkbox is checked, but it doesn't even reach the alert box even if you checked or did not check it, and i want it to display an error if there is no checkbox checked.. can anyone tell me what have i done wrong.please cause it looks 110% correct to me. 我认为如果没有选中复选框,系统将只进入警报框,但即使您选中或未检查它,它甚至都没有到达警报框,如果没有复选框,我希望它显示错误检查..任何人都可以告诉我,我做错了什么。请给我看起来110%正确。

the condition in your for loop is wrong, use: 你的for循环中的条件是错误的,使用:

for(var i = 0; i < ab.length; i++) {

< and not <= <而不是<=

edit: 编辑:

what's this: document.getElementById("repo_document_form").elements[['stands[]']]; 这是什么: document.getElementById("repo_document_form").elements[['stands[]']]; ?

that doesn't make sense at all to me … 对我来说根本没有意义......
i suspect the error is somewhere around that line. 我怀疑错误是在那条线附近。 ( elements[[ looks really weird—is your loop entered at all?) elements[[看起来很奇怪 - 你的循环输入了吗?)

The other answers are great, but they left out one thing. 其他答案很棒,但他们遗漏了一件事。 You gotta check to ensure that the ab is infact an array. 你必须检查以确保ab实际上是一个数组。 If there is only 1 checkbox, it will come back as a singular HTML Element. 如果只有一个复选框,它将作为单个HTML元素返回。

function checkstanddocument(){
 var hasOne = false;
 ab = document.getElementById("repo_document_form").elements[['stands[]']];   
 if (ab.length != null){
    for (var i = 0; i < ab.length; i++){

      if (ab[i].checked == true){
        hasOne = true;
      }    
    }
 }
 else hasOne = ab.checked;

  if (hasOne == false) {

   alert("Please select at least one stand");
   return false

  } else {

   return true;

  }

}
function checkstanddocument(){
 var hasOne = false;
 ab = document.getElementById("repo_document_form").elements[['stands[]']];   
  for (var i = 0; i < ab.length; i++){

    if (ab[i].checked == true){
      hasOne = true;
    }    
 }  

  if (hasOne == false) {

   alert("Please select at least one stand");
   return false

  } else {

   return true;

  }

}

I find that PrototypeJS makes this sort of thing very easy using the $$() function with a CSS selector to get the controls you need. 我发现PrototypeJS使用带有CSS选择器的$$()函数可以很容易地获得所需的控件。

This has the advantage of only looping over as many checkboxes are actually found so you don't have to worry about all of the loop edge conditions. 这样做的好处是只能循环实际找到的复选框,因此您不必担心所有的循环边缘条件。

This function will display an alert if none of the checkboxes are checked (or if no checkboxes are found) and will also return true or false depending on what it finds. 如果未选中任何复选框(或未找到复选框),此函数将显示警报,并且还将返回true或false,具体取决于它找到的内容。

function checkstanddocument() {
    var ret = false;

    $$('#repo_document_form input[type="checkbox"][name="stands[]"]').each( function(checkbox) {
        if(checkbox.checked) {
            ret = true;
            throw $break;
        }
    });

    if(!ret) {
        alert('Please select at least one stand.');
    }

    return ret;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM