简体   繁体   English

提交前选中所有复选框

[英]Check all checkboxes before submit

Well, I have a form where the user can select an item with a checkbox, the user type how many items he wants to send with an input text, but there are some items that requires a serial number that a checkbox holds. 好吧,我有一个表单,用户可以在其中选择一个带有复选框的项目,用户可以通过输入文本键入他想发送多少个项目,但是有些项目需要复选框具有的序列号。

This is my approach: 这是我的方法:

<script>
      function checkall()
      {
        var ok=true;
        $('input:checkbox[id^="art"]:checked').each(function() {
          var vals = $(this).val();
          //first attempt
          if (typeof document.getElementById(vals+"existencia") === 'undefined') {
            ok=false;
            return false;
            alert("Error message");
          }else
          if (typeof document.getElementById(vals+"-serie")==='undefined') {
            ok=false
            return false;
            alert("Error message 2");
          }

//second attempt
          if (obj.hasOwnProperty(vals+'existencia')) {
            art = document.getElementById(vals+"existencia").value;
            alert(art);
          }else if (obj.hasOwnProperty(vals+'serial')) {
            sert=document.getElementById(vals+"-serie").value;
            alert(sert);
          }
        });


        if(ok)
        {
          document.getElementById("myform").submit();
        }
      }
      </script>

The script is intended to loop all checked checkboxes and if the input is empty stops and alerts the user and the other stage is when he checks an article with serial number, at least 1 have to checked and also alerts the client 该脚本旨在循环所有选中的复选框,如果输入为空,则停止并警告用户,另一阶段是当他检查序列号的文章时,至少要选中1并还警告客户端

1号

The first attempt, I tried, got passed cause those elements are defined and second attempt throws obj is not defined , maybe I missunderstood the codes and answers I searched, can someone help to achieve my goal or explain me what I did wrong, thank you. 我尝试过的第一次尝试通过了,因为定义了那些元素,而第二次尝试未定义obj is not defined抛出,也许我误解了我搜索的代码和答案,有人可以帮助实现我的目标或向我解释我做错了什么,谢谢。

UPDATE: 更新:

if ($inicio == 0) {
                    echo "<tr bgcolor= '#f4f4f4' >";
                    $inicio = 1;
                  } else {
                    echo "<tr bgcolor= white>";
                    $inicio = 0;
                  }
                  ?>
                  <td style="width: 10%;text-align: center;">
                    <input type="checkbox" name="art[]" id="art" value="<?php echo $articulo; ?>" /></td>
                    <td width="12%" align="center"><?php echo $diferencia_dias ?> d&iacute;as</td>
                    <td width="8%" style="text-align: center;"><?php echo $articulo; ?></td>

                    <td width="45%"><?php echo $descripcion; ?></td>
                    <?php
                    if($numserie>0){
                      $esto = trim($articulo);
                      $bus_serie = mssql_query("execute serial_madness '$esto','$almacen'");
                      echo "<td style='width:10%;text-align:left;'>";
                      while($res_serie = mssql_fetch_array($bus_serie))
                      {
                        echo '<input type="checkbox" id="'.$articulo.'-serie" name="'.$articulo.'-Serie[]" value="'.$res_serie["ARTICULO"].','.$res_serie["NUMSERIE"].'_'.$valor.'  "> '.$res_serie["NUMSERIE"].' <br>';
                      }
                      echo "</td>";
                    }else{
                      ?><td style="width:10%;text-align:center;"><input type="number" style="text-align:center;width:30px; height:30px;" id="<?php echo $articulo_c; ?>existencia" name="<?php echo
                      $articulo_c; ?>existencia" value="<?php echo (int)$existencias; ?>" min="1" max="<?php echo (int)$existencias; ?>"/></td>
                      <?php
                    } ?>

So, in order to check a checkbox you can just use jquery. 因此,为了选中一个复选框,您可以只使用jquery。

Example: $("#checkboxId").prop('checked', true); 示例: $("#checkboxId").prop('checked', true);

The example above will check the checkbox with id "checkboxId", changing the true to false will uncheck the box; 上面的示例将选中ID为“ checkboxId”的复选框,将true更改为false将取消选中该复选框;

If you want to check how many or which inputs are checked you can use the .length of the array returned by the jquery selector. 如果要检查检查了多少个或哪些输入,则可以使用jquery选择器返回的数组的.length。

The following will check if all the checkboxes with ids starting with the word "test" are checked: 下面将检查是否选中了所有以“ test”开头的ID的复选框:

if($('[id^="test"]:checkbox').length == $('[id^="test"]:checkbox:checked').length) 
{
   // Do stuff here
}

Likewise you can check if nothing is checked if the length is equal to zero. 同样,如果长度等于零,则可以检查是否不进行任何检查。

I prepared a simple example that I think tackles the general issue you are solving here. 我准备了一个简单的示例,我认为它可以解决您正在此处解决的一般问题。

https://plnkr.co/edit/2v6x1DXRpAiaKiBFSMz6?p=preview https://plnkr.co/edit/2v6x1DXRpAiaKiBFSMz6?p=preview

Notice that when you click 'Submit' in my plunker example all checkboxes will automatically become checked. 请注意,当您在插件示例中单击“提交”时,所有复选框将自动变为选中状态。

If you click the 'Validate' button it will verify that all checkboxes are indeed checked. 如果单击“验证”按钮,它将验证是否确实选中了所有复选框。

You should be able to use my example to create a solution for your specific case. 您应该可以使用我的示例为您的特定案例创建解决方案。

You may also want to use the .is() jquery function example: $('#' + id).is(":checked") 您可能还想使用.is() jQuery函数示例: $('#' + id).is(":checked")

See the following answer for some similar solutions and explanations: Check if checkbox is checked with jQuery 有关类似的解决方案和解释,请参见以下答案: 检查jQuery是否选中了复选框

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

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